Most visited

Recently visited

Added in API level 18

NotificationListenerService

public abstract class NotificationListenerService
extends Service

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Service
         ↳ android.service.notification.NotificationListenerService


A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.

To extend this class, you must declare the service in your manifest file with the BIND_NOTIFICATION_LISTENER_SERVICE permission and include an intent filter with the SERVICE_INTERFACE action. For example:

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

The service should wait for the onListenerConnected() event before performing any operations. The requestRebind(ComponentName) method is the only one that is safe to call before onListenerConnected() or after onListenerDisconnected().

Summary

Nested classes

class NotificationListenerService.Ranking

Stores ranking related information on a currently active notification. 

class NotificationListenerService.RankingMap

Provides access to ranking information on currently active notifications. 

Constants

int HINT_HOST_DISABLE_CALL_EFFECTS

Listener hints constant - the primary device UI should disable phone call sounds, buyt not notification sound.

int HINT_HOST_DISABLE_EFFECTS

Listener hints constant - the primary device UI should disable notification sound, vibrating and other visual or aural effects.

int HINT_HOST_DISABLE_NOTIFICATION_EFFECTS

Listener hints constant - the primary device UI should disable notification sound, but not phone calls.

int INTERRUPTION_FILTER_ALARMS

Interruption filter constant - Alarms only interruption filter.

int INTERRUPTION_FILTER_ALL

Interruption filter constant - Normal interruption filter.

int INTERRUPTION_FILTER_NONE

Interruption filter constant - No interruptions filter.

int INTERRUPTION_FILTER_PRIORITY

Interruption filter constant - Priority interruption filter.

int INTERRUPTION_FILTER_UNKNOWN

Interruption filter constant - returned when the value is unavailable for any reason.

String SERVICE_INTERFACE

The Intent that must be declared as handled by the service.

int SUPPRESSED_EFFECT_SCREEN_OFF

Whether notification suppressed by DND should not interruption visually when the screen is off.

int SUPPRESSED_EFFECT_SCREEN_ON

Whether notification suppressed by DND should not interruption visually when the screen is on.

Inherited constants

From class android.app.Service
From class android.content.Context
From interface android.content.ComponentCallbacks2

Public constructors

NotificationListenerService()

Public methods

final void cancelAllNotifications()

Inform the notification manager about dismissal of all notifications.

final void cancelNotification(String pkg, String tag, int id)

This method was deprecated in API level 21. Use cancelNotification(String) instead. Beginning with LOLLIPOP this method will no longer cancel the notification. It will continue to cancel the notification for applications whose targetSdkVersion is earlier than LOLLIPOP.

final void cancelNotification(String key)

Inform the notification manager about dismissal of a single notification.

final void cancelNotifications(String[] keys)

Inform the notification manager about dismissal of specific notifications.

StatusBarNotification[] getActiveNotifications(String[] keys)

Request one or more notifications by key.

StatusBarNotification[] getActiveNotifications()

Request the list of outstanding notifications (that is, those that are visible to the current user).

final int getCurrentInterruptionFilter()

Gets the current notification interruption filter active on the host.

final int getCurrentListenerHints()

Gets the set of hints representing current state.

NotificationListenerService.RankingMap getCurrentRanking()

Returns current ranking information.

IBinder onBind(Intent intent)

This is not the lifecycle event you are looking for.

void onDestroy()

Called by the system to notify a Service that it is no longer used and is being removed.

void onInterruptionFilterChanged(int interruptionFilter)

Implement this method to be notified when the interruption filter changed.

void onListenerConnected()

Implement this method to learn about when the listener is enabled and connected to the notification manager.

void onListenerDisconnected()

Implement this method to learn about when the listener is disconnected from the notification manager.You will not receive any events after this call, and may only call requestRebind(ComponentName) at this time.

void onListenerHintsChanged(int hints)

Implement this method to be notified when the Listener hints change.

void onNotificationPosted(StatusBarNotification sbn, NotificationListenerService.RankingMap rankingMap)

Implement this method to learn about new notifications as they are posted by apps.

void onNotificationPosted(StatusBarNotification sbn)

Implement this method to learn about new notifications as they are posted by apps.

void onNotificationRankingUpdate(NotificationListenerService.RankingMap rankingMap)

Implement this method to be notified when the notification ranking changes.

void onNotificationRemoved(StatusBarNotification sbn)

Implement this method to learn when notifications are removed.

void onNotificationRemoved(StatusBarNotification sbn, NotificationListenerService.RankingMap rankingMap)

Implement this method to learn when notifications are removed.

final void requestInterruptionFilter(int interruptionFilter)

Sets the desired interruption filter.

final void requestListenerHints(int hints)

Sets the desired listener hints.

static void requestRebind(ComponentName componentName)

Request that the listener be rebound, after a previous call to (@link requestUnbind).

final void requestUnbind()

Request that the service be unbound.

final void setNotificationsShown(String[] keys)

Inform the notification manager that these notifications have been viewed by the user.

Protected methods

void attachBaseContext(Context base)

Set the base context for this ContextWrapper.

Inherited methods

From class android.app.Service
From class android.content.ContextWrapper
From class android.content.Context
From class java.lang.Object
From interface android.content.ComponentCallbacks2
From interface android.content.ComponentCallbacks

Constants

HINT_HOST_DISABLE_CALL_EFFECTS

Added in API level 24
int HINT_HOST_DISABLE_CALL_EFFECTS

Listener hints constant - the primary device UI should disable phone call sounds, buyt not notification sound. This does not change the interruption filter, only the effects.

Constant Value: 4 (0x00000004)

HINT_HOST_DISABLE_EFFECTS

Added in API level 21
int HINT_HOST_DISABLE_EFFECTS

Listener hints constant - the primary device UI should disable notification sound, vibrating and other visual or aural effects. This does not change the interruption filter, only the effects.

Constant Value: 1 (0x00000001)

HINT_HOST_DISABLE_NOTIFICATION_EFFECTS

Added in API level 24
int HINT_HOST_DISABLE_NOTIFICATION_EFFECTS

Listener hints constant - the primary device UI should disable notification sound, but not phone calls. This does not change the interruption filter, only the effects.

Constant Value: 2 (0x00000002)

INTERRUPTION_FILTER_ALARMS

Added in API level 23
int INTERRUPTION_FILTER_ALARMS

Interruption filter constant - Alarms only interruption filter.

Constant Value: 4 (0x00000004)

INTERRUPTION_FILTER_ALL

Added in API level 21
int INTERRUPTION_FILTER_ALL

Interruption filter constant - Normal interruption filter.

Constant Value: 1 (0x00000001)

INTERRUPTION_FILTER_NONE

Added in API level 21
int INTERRUPTION_FILTER_NONE

Interruption filter constant - No interruptions filter.

Constant Value: 3 (0x00000003)

INTERRUPTION_FILTER_PRIORITY

Added in API level 21
int INTERRUPTION_FILTER_PRIORITY

Interruption filter constant - Priority interruption filter.

Constant Value: 2 (0x00000002)

INTERRUPTION_FILTER_UNKNOWN

Added in API level 23
int INTERRUPTION_FILTER_UNKNOWN

Interruption filter constant - returned when the value is unavailable for any reason. For example, before the notification listener is connected.

See also:

Constant Value: 0 (0x00000000)

SERVICE_INTERFACE

Added in API level 18
String SERVICE_INTERFACE

The Intent that must be declared as handled by the service.

Constant Value: "android.service.notification.NotificationListenerService"

SUPPRESSED_EFFECT_SCREEN_OFF

Added in API level 24
int SUPPRESSED_EFFECT_SCREEN_OFF

Whether notification suppressed by DND should not interruption visually when the screen is off.

Constant Value: 1 (0x00000001)

SUPPRESSED_EFFECT_SCREEN_ON

Added in API level 24
int SUPPRESSED_EFFECT_SCREEN_ON

Whether notification suppressed by DND should not interruption visually when the screen is on.

Constant Value: 2 (0x00000002)

Public constructors

NotificationListenerService

Added in API level 18
NotificationListenerService ()

Public methods

cancelAllNotifications

Added in API level 18
void cancelAllNotifications ()

Inform the notification manager about dismissal of all notifications.

Use this if your listener has a user interface that allows the user to dismiss all notifications, similar to the behavior of Android's status bar and notification panel. It should be called after the user invokes the "dismiss all" function of your UI; upon being informed, the notification manager will actually remove all active notifications and you will get multiple onNotificationRemoved(StatusBarNotification) callbacks.

The service should wait for the onListenerConnected() event before performing this operation.

See also:

cancelNotification

Added in API level 18
void cancelNotification (String pkg, 
                String tag, 
                int id)

This method was deprecated in API level 21.
Use cancelNotification(String) instead. Beginning with LOLLIPOP this method will no longer cancel the notification. It will continue to cancel the notification for applications whose targetSdkVersion is earlier than LOLLIPOP.

Inform the notification manager about dismissal of a single notification.

Use this if your listener has a user interface that allows the user to dismiss individual notifications, similar to the behavior of Android's status bar and notification panel. It should be called after the user dismisses a single notification using your UI; upon being informed, the notification manager will actually remove the notification and you will get an onNotificationRemoved(StatusBarNotification) callback.

Note: If your listener allows the user to fire a notification's contentIntent by tapping/clicking/etc., you should call this method at that time if the Notification in question has the FLAG_AUTO_CANCEL flag set.

The service should wait for the onListenerConnected() event before performing this operation.

Parameters
pkg String: Package of the notifying app.
tag String: Tag of the notification as specified by the notifying app in notify(String, int, android.app.Notification).
id int: ID of the notification as specified by the notifying app in notify(String, int, android.app.Notification).

cancelNotification

Added in API level 21
void cancelNotification (String key)

Inform the notification manager about dismissal of a single notification.

Use this if your listener has a user interface that allows the user to dismiss individual notifications, similar to the behavior of Android's status bar and notification panel. It should be called after the user dismisses a single notification using your UI; upon being informed, the notification manager will actually remove the notification and you will get an onNotificationRemoved(StatusBarNotification) callback.

Note: If your listener allows the user to fire a notification's contentIntent by tapping/clicking/etc., you should call this method at that time if the Notification in question has the FLAG_AUTO_CANCEL flag set.

The service should wait for the onListenerConnected() event before performing this operation.

Parameters
key String: Notification to dismiss from getKey().

cancelNotifications

Added in API level 21
void cancelNotifications (String[] keys)

Inform the notification manager about dismissal of specific notifications.

Use this if your listener has a user interface that allows the user to dismiss multiple notifications at once.

The service should wait for the onListenerConnected() event before performing this operation.

Parameters
keys String: Notifications to dismiss, or null to dismiss all.

getActiveNotifications

Added in API level 21
StatusBarNotification[] getActiveNotifications (String[] keys)

Request one or more notifications by key. Useful if you have been keeping track of notifications but didn't want to retain the bits, and now need to go back and extract more data out of those notifications.

The service should wait for the onListenerConnected() event before performing this operation.

Parameters
keys String: the keys of the notifications to request
Returns
StatusBarNotification[] An array of notifications corresponding to the requested keys, in the same order as the key list.

getActiveNotifications

Added in API level 18
StatusBarNotification[] getActiveNotifications ()

Request the list of outstanding notifications (that is, those that are visible to the current user). Useful when you don't know what's already been posted.

The service should wait for the onListenerConnected() event before performing this operation.

Returns
StatusBarNotification[] An array of active notifications, sorted in natural order.

getCurrentInterruptionFilter

Added in API level 21
int getCurrentInterruptionFilter ()

Gets the current notification interruption filter active on the host.

The interruption filter defines which notifications are allowed to interrupt the user (e.g. via sound & vibration) and is applied globally. Listeners can find out whether a specific notification matched the interruption filter via matchesInterruptionFilter().

The current filter may differ from the previously requested filter if the notification host does not support or refuses to apply the requested filter, or if another component changed the filter in the meantime.

Listen for updates using onInterruptionFilterChanged(int).

The service should wait for the onListenerConnected() event before performing this operation.

Returns
int One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when unavailable.

getCurrentListenerHints

Added in API level 21
int getCurrentListenerHints ()

Gets the set of hints representing current state.

The current state may differ from the requested state if the hint represents state shared across all listeners or a feature the notification host does not support or refuses to grant.

The service should wait for the onListenerConnected() event before performing this operation.

Returns
int Zero or more of the HINT_ constants.

getCurrentRanking

Added in API level 21
NotificationListenerService.RankingMap getCurrentRanking ()

Returns current ranking information.

The returned object represents the current ranking snapshot and only applies for currently active notifications.

Generally you should use the RankingMap that is passed with events such as onNotificationPosted(StatusBarNotification, RankingMap), onNotificationRemoved(StatusBarNotification, RankingMap), and so on. This method should only be used when needing access outside of such events, for example to retrieve the RankingMap right after initialization.

The service should wait for the onListenerConnected() event before performing this operation.

Returns
NotificationListenerService.RankingMap A NotificationListenerService.RankingMap object providing access to ranking information

onBind

Added in API level 18
IBinder onBind (Intent intent)

This is not the lifecycle event you are looking for.

The service should wait for the onListenerConnected() event before performing any operations.

Parameters
intent Intent: The Intent that was used to bind to this service, as given to Context.bindService. Note that any extras that were included with the Intent at that point will not be seen here.
Returns
IBinder Return an IBinder through which clients can call on to the service.

onDestroy

Added in API level 18
void onDestroy ()

Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up any resources it holds (threads, registered receivers, etc) at this point. Upon return, there will be no more calls in to this Service object and it is effectively dead. Do not call this method directly.

onInterruptionFilterChanged

Added in API level 21
void onInterruptionFilterChanged (int interruptionFilter)

Implement this method to be notified when the interruption filter changed.

Parameters
interruptionFilter int: The current interruption filter.

onListenerConnected

Added in API level 21
void onListenerConnected ()

Implement this method to learn about when the listener is enabled and connected to the notification manager. You are safe to call getActiveNotifications() at this time.

onListenerDisconnected

Added in API level 24
void onListenerDisconnected ()

Implement this method to learn about when the listener is disconnected from the notification manager.You will not receive any events after this call, and may only call requestRebind(ComponentName) at this time.

onListenerHintsChanged

Added in API level 21
void onListenerHintsChanged (int hints)

Implement this method to be notified when the Listener hints change.

Parameters
hints int: The current listener hints.

onNotificationPosted

Added in API level 21
void onNotificationPosted (StatusBarNotification sbn, 
                NotificationListenerService.RankingMap rankingMap)

Implement this method to learn about new notifications as they are posted by apps.

Parameters
sbn StatusBarNotification: A data structure encapsulating the original Notification object as well as its identifying information (tag and id) and source (package name).
rankingMap NotificationListenerService.RankingMap: The current ranking map that can be used to retrieve ranking information for active notifications, including the newly posted one.

onNotificationPosted

Added in API level 18
void onNotificationPosted (StatusBarNotification sbn)

Implement this method to learn about new notifications as they are posted by apps.

Parameters
sbn StatusBarNotification: A data structure encapsulating the original Notification object as well as its identifying information (tag and id) and source (package name).

onNotificationRankingUpdate

Added in API level 21
void onNotificationRankingUpdate (NotificationListenerService.RankingMap rankingMap)

Implement this method to be notified when the notification ranking changes.

Parameters
rankingMap NotificationListenerService.RankingMap: The current ranking map that can be used to retrieve ranking information for active notifications.

onNotificationRemoved

Added in API level 18
void onNotificationRemoved (StatusBarNotification sbn)

Implement this method to learn when notifications are removed.

This might occur because the user has dismissed the notification using system UI (or another notification listener) or because the app has withdrawn the notification.

NOTE: The StatusBarNotification object you receive will be "light"; that is, the result from getNotification() may be missing some heavyweight fields such as contentView and largeIcon. However, all other fields on StatusBarNotification, sufficient to match this call with a prior call to onNotificationPosted(StatusBarNotification), will be intact.

Parameters
sbn StatusBarNotification: A data structure encapsulating at least the original information (tag and id) and source (package name) used to post the Notification that was just removed.

onNotificationRemoved

Added in API level 21
void onNotificationRemoved (StatusBarNotification sbn, 
                NotificationListenerService.RankingMap rankingMap)

Implement this method to learn when notifications are removed.

This might occur because the user has dismissed the notification using system UI (or another notification listener) or because the app has withdrawn the notification.

NOTE: The StatusBarNotification object you receive will be "light"; that is, the result from getNotification() may be missing some heavyweight fields such as contentView and largeIcon. However, all other fields on StatusBarNotification, sufficient to match this call with a prior call to onNotificationPosted(StatusBarNotification), will be intact.

Parameters
sbn StatusBarNotification: A data structure encapsulating at least the original information (tag and id) and source (package name) used to post the Notification that was just removed.
rankingMap NotificationListenerService.RankingMap: The current ranking map that can be used to retrieve ranking information for active notifications.

requestInterruptionFilter

Added in API level 21
void requestInterruptionFilter (int interruptionFilter)

Sets the desired interruption filter.

This is merely a request, the host may or may not choose to apply the requested interruption filter depending on other listener requests or other global state.

Listen for updates using onInterruptionFilterChanged(int).

The service should wait for the onListenerConnected() event before performing this operation.

Parameters
interruptionFilter int: One of the INTERRUPTION_FILTER_ constants.

requestListenerHints

Added in API level 21
void requestListenerHints (int hints)

Sets the desired listener hints.

This is merely a request, the host may or may not choose to take action depending on other listener requests or other global state.

Listen for updates using onListenerHintsChanged(int).

The service should wait for the onListenerConnected() event before performing this operation.

Parameters
hints int: One or more of the HINT_ constants.

requestRebind

Added in API level 24
void requestRebind (ComponentName componentName)

Request that the listener be rebound, after a previous call to (@link requestUnbind).

This method will fail for listeners that have not been granted the permission by the user.

Parameters
componentName ComponentName

requestUnbind

Added in API level 24
void requestUnbind ()

Request that the service be unbound.

This will no longer receive updates until requestRebind(ComponentName) is called. The service will likely be kiled by the system after this call.

The service should wait for the onListenerConnected() event before performing this operation. I know it's tempting, but you must wait.

setNotificationsShown

Added in API level 23
void setNotificationsShown (String[] keys)

Inform the notification manager that these notifications have been viewed by the user. This should only be called when there is sufficient confidence that the user is looking at the notifications, such as when the notifications appear on the screen due to an explicit user interaction.

The service should wait for the onListenerConnected() event before performing this operation.

Parameters
keys String: Notifications to mark as seen.

Protected methods

attachBaseContext

Added in API level 18
void attachBaseContext (Context base)

Set the base context for this ContextWrapper. All calls will then be delegated to the base context. Throws IllegalStateException if a base context has already been set.

Parameters
base Context: The new base context for this wrapper.

Hooray!