Scheduling notifications
suggest changeSometimes it is required to display a notification at a specific time, a task that unfortunately is not trivial on the Android system, as there is no method setTime() or similar for notifications. On Android 8.0 and higher the notification also needs a channel id, which this sample does not set. setExactAndAllowWhileIdle exists from API 23. This example outlines the steps needed to schedule notifications using the AlarmManager:
- Add a
BroadcastReceiverthat listens toIntents broadcasted by the AndroidAlarmManager.
This is the place where you build your notification based on the extras provided with the Intent:
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Build notification based on Intent
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification_small_icon)
.setContentTitle(intent.getStringExtra("title"))
.setContentText(intent.getStringExtra("text"))
.build();
// Show notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(42, notification);
}
}
- Register the
BroadcastReceiverin yourAndroidManifest.xmlfile (otherwise the receiver won’t receive anyIntents from theAlarmManager):
<receiver
android:name=".NotificationReceiver"
android:enabled="true" />
- Schedule a notification by passing a
PendingIntentfor yourBroadcastReceiverwith the neededIntentextras to the systemAlarmManager. YourBroadcastReceiverwill receive theIntentonce the given time has arrived and display the notification. The following method schedules a notification:
public static void scheduleNotification(Context context, long time, String title, String text) {
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra("title", title);
intent.putExtra("text", text);
PendingIntent pending = PendingIntent.getBroadcast(context, 42, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Schedule notification
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pending);
}
Intent has no two-argument getStringExtra, so a missing extra is null. The 42 above needs to be unique for each scheduled notification, otherwise the PendingIntents will replace each other.
- Cancel a notification by rebuilding the associated
PendingIntentand canceling it on the systemAlarmManager. The following method cancels a notification:
public static void cancelNotification(Context context, String title, String text) {
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra("title", title);
intent.putExtra("text", text);
PendingIntent pending = PendingIntent.getBroadcast(context, 42, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Cancel notification
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.cancel(pending);
}
Note that the 42 above needs to match the number from step 3!