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
        NotificationCompat.Builder(this)   
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Notification Title")
            .setContentText("Notification Message")
            .setTicker("Ticker Text")
            .setAutoCancel(true)       
            .setNumber(1)
            .build();
       
    //---set the sound and lights---
    notification.defaults |=
        Notification.DEFAULT_SOUND;
    notification.defaults |=
        Notification.DEFAULT_LIGHTS;

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---
    notificationManager.notify("tag",
        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.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class NotificationListener extends
    NotificationListenerService {

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

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

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

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:

    this.cancelNotification(
        notif.getPackageName(),
        notif.getTag(),
        notif.getId());

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

    //---remove all notifications---       
    this.cancelAllNotifications();

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

<service android:name=".NotificationListener"
    android:label="@string/app_name"
    android:permission=        
    "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name=
 "android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

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: