https://developer.android.com/reference/android/app/Notification.html
public void showNotification(String title, String message) { Intent notifyIntent = new Intent(this, MainActivity.class); notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{notifyIntent}, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new Notification.Builder(this) .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle(title) .setContentText(message) .setAutoCancel(true) .setContentIntent(pendingIntent) .build(); notification.defaults |= Notification.DEFAULT_SOUND; NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notification); }
簡易使用。
開個button 點擊後,notification通知就出來了。
可以設定在alarmManager做配合使用
比如定點定時,做出推播效果提醒使用者。
沒有留言:
張貼留言