Sunday, March 30, 2014

Android Tip - Notification Listener Service

Android 4.3 introduces a new API called the Notification Listener Service.  Its main function is to let applications have the ability to be notified when notifications are posted or removed from the system. A good example use of this is the Android Wear application, which receives all the notifications posted on your Android device and routes them to your Android Wear device.

To create a notification, you can use the NotificationCompat.Builder class:

    //---create a notification---
    Notification notification = new
            .setContentTitle("Notification Title")
            .setContentText("Notification Message")
            .setTicker("Ticker Text")
    //---set the sound and lights---
    notification.defaults |=
    notification.defaults |=

To post a notification on your Android device, you can use the NotificationManager class’s notify() method, passing it a tag, notification ID, as well as the Notification object:
    NotificationManager notificationManager;  
    int notificationID = 0;

    //---display the notification---
        notificationID++, notification);   

To listen for notifications posted on the Android device, you need to create a new class that extends the NotificationListenerService base class:

package net.learn2develop.notificationservice;

import android.service.notification.StatusBarNotification;
import android.util.Log;

public class NotificationListener extends
    NotificationListenerService {

    public void onNotificationPosted(
        StatusBarNotification sbn) {
        //---show current notification---
        Log.i("","---Current Notification---");
        Log.i("","ID :" + sbn.getId() + "\t" +
            sbn.getNotification().tickerText + "\t" +

        //---show all active notifications---
        Log.i("","===All Notifications===");
        for (StatusBarNotification notif :
            this.getActiveNotifications()) {           
            Log.i("","ID :" + notif.getId() + "\t" +
                notif.getNotification().tickerText + "\t" +

    public void onNotificationRemoved(
        StatusBarNotification sbn) {
        Log.i("","---Notification Removed---");
        Log.i("","ID :" + sbn.getId() + "\t" +
            sbn.getNotification().tickerText + "\t" +

You need to override two methods in this class:
·       onNotificationPosted() – called when a notification is received by the system
·       onNotificationRemoved() – called when a notification is removed

For these two methods, an instance of the StatusBarNotification class will be passed in, allowing you to know which application posted the notification, its content, id, etc.

Within the service, you can also list all the notifications currently available in the notification bar by using the getActiveNotifications() method. You can also remove a particular notification by using the cancelNotification() method, passing it the package name of the application that posted the notification, the tag of the notification, as well as its notification id:


To remove all notifications, use the cancelAllNotifications() method:

    //---remove all notifications---       

Finally, you need to declare your service in the AndroidManifest.xml file:

<service android:name=".NotificationListener"
        <action android:name=
 "android.service.notification.NotificationListenerService" />

When the application is installed on the device, the user must explicitly give permission to your app to listen for notifications on the system. They need to do so via the Notification access page at: Settings | Security | Notification access:

No comments: