Scheduling notifications

suggest change

Sometimes 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 similiar for notifications. This example outlines the steps needed to schedule notifications using the AlarmManager:

  1. Add a BroadcastReceiver that listens to Intents broadcasted by the Android AlarmManager.

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);
    }
}
  1. Register the BroadcastReceiver in your AndroidManifest.xml file (otherwise the receiver won’t receive any Intents from the AlarmManager):
<receiver
    android:name=".NotificationReceiver"
    android:enabled="true" />
  1. Schedule a notification by passing a PendingIntent for your BroadcastReceiver with the needed Intent extras to the system AlarmManager. Your BroadcastReceiver will receive the Intent once 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);
    // Schdedule notification
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pending);
}

*Please note that the `42` above needs to be unique for each scheduled notification, otherwise the `PendingIntent`s will replace each other causing undesired effects!*
  1. Cancel a notification by rebuilding the associated PendingIntent and canceling it on the system AlarmManager. 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!

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Notifications:
* Scheduling notifications

Table Of Contents
2 Gradle
5 Intent
17 Service
19 WebView
31 SQLite
35 Glide
37 Dialog
38 ACRA
41 Notifications
44 Handler
53 Toast
63 Menu
65 Picasso
70 Volley
71 Widgets
78 Realm
90 Spinner
95 OkHttp
108 TextView
109 ListView
111 Loader
118 Xposed
119 Security
121 ImageView
123 Doze Mode
130 Drawables
131 Colors
134 Fresco
139 AdMob
145 Keyboard
146 Button
150 EditText
155 Vk SDK
163 ExoPlayer
169 XMPP
175 OpenCV
177 Threads
184 ORMLite
186 TabLayout
190 LruCache
192 Zip files
194 Fastlane
199 FileIO
202 Moshi
210 VideoView
216 Paint
218 ProGuard
226 CleverTap
228 ADB shell
229 Ping ICMP
230 AIDL
234 Context
240 JCodec
242 Okio
249 FuseView
254 Looper
261 Fastjson
263 Jackson
267 Smartcard