Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

NotificationAssistantService

public abstract class NotificationAssistantService
extends NotificationListenerService

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


A service that helps the user manage notifications.

Only one notification assistant can be active at a time. Unlike notification listener services, assistant services can additionally modify certain aspects about notifications (see Adjustment) before they are posted.

A note about managed profiles: Unlike NotificationListenerService, NotificationAssistantServices are allowed to run in managed profiles (see DevicePolicyManager#isManagedProfile(ComponentName)), so they can access the information they need to create good Adjustment. To maintain the contract with NotificationListenerService, an assistant service will receive all of the callbacks from NotificationListenerService for the current user, managed profiles of that user, and ones that affect all users. However, onNotificationEnqueued(android.service.notification.StatusBarNotification) will only be called for notifications sent to the current user, and Adjustment will only be accepted for the current user.

All callbacks are called on the main thread.

Summary

Constants

String SERVICE_INTERFACE

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

int SOURCE_FROM_APP

int SOURCE_FROM_ASSISTANT

Inherited constants

Public constructors

NotificationAssistantService()

Public methods

final void adjustNotification(Adjustment adjustment)

Updates a notification.

final void adjustNotifications(List<Adjustment> adjustments)

Updates existing notifications.

void onActionInvoked(String key, Notification.Action action, int source)

Implement this to know when an action is clicked.

final IBinder onBind(Intent intent)

This is not the lifecycle event you are looking for.

void onNotificationDirectReplied(String key)

Implement this to know when a direct reply is sent from a notification.

Adjustment onNotificationEnqueued(StatusBarNotification sbn, NotificationChannel channel)

A notification was posted by an app.

abstract Adjustment onNotificationEnqueued(StatusBarNotification sbn)

A notification was posted by an app.

void onNotificationExpansionChanged(String key, boolean isUserAction, boolean isExpanded)

Implement this to know when a notification change (expanded / collapsed) is visible to user.

void onNotificationRemoved(StatusBarNotification sbn, NotificationListenerService.RankingMap rankingMap, NotificationStats stats, int reason)

Implement this method to learn when notifications are removed, how they were interacted with before removal, and why they were removed.

abstract void onNotificationSnoozedUntilContext(StatusBarNotification sbn, String snoozeCriterionId)

A notification was snoozed until a context.

void onNotificationsSeen(List<String> keys)

Implement this to know when a user has seen notifications, as triggered by NotificationListenerService.setNotificationsShown(java.lang.String[]).

void onSuggestedReplySent(String key, CharSequence reply, int source)

Implement this to know when a suggested reply is sent.

final void unsnoozeNotification(String key)

Inform the notification manager about un-snoozing a specific notification.

Protected methods

void attachBaseContext(Context base)

Set the base context for this ContextWrapper.

Inherited methods

Constants

SERVICE_INTERFACE

public static final String SERVICE_INTERFACE

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

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

SOURCE_FROM_APP

public static final int SOURCE_FROM_APP

Constant Value: 0 (0x00000000)

SOURCE_FROM_ASSISTANT

public static final int SOURCE_FROM_ASSISTANT

Constant Value: 1 (0x00000001)

Public constructors

NotificationAssistantService

public NotificationAssistantService ()

Public methods

adjustNotification

public final void adjustNotification (Adjustment adjustment)

Updates a notification. N.B. this won’t cause an existing notification to alert, but might allow a future update to this notification to alert.

Parameters
adjustment Adjustment: the adjustment with an explanation

adjustNotifications

public final void adjustNotifications (List<Adjustment> adjustments)

Updates existing notifications. Re-ranking won't occur until all adjustments are applied. N.B. this won’t cause an existing notification to alert, but might allow a future update to these notifications to alert.

Parameters
adjustments List: a list of adjustments with explanations

onActionInvoked

public void onActionInvoked (String key, 
                Notification.Action action, 
                int source)

Implement this to know when an action is clicked.

Parameters
key String: the notification key This value must never be null.

action Notification.Action: the action that is just clicked This value must never be null.

source int: the source that provided the action, e.g. SOURCE_FROM_APP Value is SOURCE_FROM_APP, or SOURCE_FROM_ASSISTANT

onBind

public final 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.

onNotificationDirectReplied

public void onNotificationDirectReplied (String key)

Implement this to know when a direct reply is sent from a notification.

Parameters
key String: the notification key This value must never be null.

onNotificationEnqueued

public Adjustment onNotificationEnqueued (StatusBarNotification sbn, 
                NotificationChannel channel)

A notification was posted by an app. Called before post.

Parameters
sbn StatusBarNotification: the new notification

channel NotificationChannel: the channel the notification was posted to

Returns
Adjustment an adjustment or null to take no action, within 100ms.

onNotificationEnqueued

public abstract Adjustment onNotificationEnqueued (StatusBarNotification sbn)

A notification was posted by an app. Called before post.

Note: this method is only called if you don't override onNotificationEnqueued(android.service.notification.StatusBarNotification, android.app.NotificationChannel).

Parameters
sbn StatusBarNotification: the new notification

Returns
Adjustment an adjustment or null to take no action, within 100ms.

onNotificationExpansionChanged

public void onNotificationExpansionChanged (String key, 
                boolean isUserAction, 
                boolean isExpanded)

Implement this to know when a notification change (expanded / collapsed) is visible to user.

Parameters
key String: the notification key This value must never be null.

isUserAction boolean: whether the expanded change is caused by user action.

isExpanded boolean: whether the notification is expanded.

onNotificationRemoved

public void onNotificationRemoved (StatusBarNotification sbn, 
                NotificationListenerService.RankingMap rankingMap, 
                NotificationStats stats, 
                int reason)

Implement this method to learn when notifications are removed, how they were interacted with before removal, and why they were 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 StatusBarNotification#getNotification may be missing some heavyweight fields such as Notification.contentView and Notification.largeIcon. However, all other fields on StatusBarNotification, sufficient to match this call with a prior call to NotificationListenerService.onNotificationPosted(android.service.notification.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.

stats NotificationStats: Stats about how the user interacted with the notification before it was removed.

reason int: see NotificationListenerService.REASON_LISTENER_CANCEL, etc.

onNotificationSnoozedUntilContext

public abstract void onNotificationSnoozedUntilContext (StatusBarNotification sbn, 
                String snoozeCriterionId)

A notification was snoozed until a context. For use with Adjustment#KEY_SNOOZE_CRITERIA. When the device reaches the given context, the assistant should restore the notification with unsnoozeNotification(java.lang.String).

Parameters
sbn StatusBarNotification: the notification to snooze

snoozeCriterionId String: the SnoozeCriterion#getId() representing a device context.

onNotificationsSeen

public void onNotificationsSeen (List<String> keys)

Implement this to know when a user has seen notifications, as triggered by NotificationListenerService.setNotificationsShown(java.lang.String[]).

Parameters
keys List

onSuggestedReplySent

public void onSuggestedReplySent (String key, 
                CharSequence reply, 
                int source)

Implement this to know when a suggested reply is sent.

Parameters
key String: the notification key This value must never be null.

reply CharSequence: the reply that is just sent This value must never be null.

source int: the source that provided the reply, e.g. SOURCE_FROM_APP Value is SOURCE_FROM_APP, or SOURCE_FROM_ASSISTANT

unsnoozeNotification

public final void unsnoozeNotification (String key)

Inform the notification manager about un-snoozing a specific notification.

This should only be used for notifications snoozed because of a contextual snooze suggestion you provided via Adjustment#KEY_SNOOZE_CRITERIA. Once un-snoozed, you will get a NotificationListenerService.onNotificationPosted(android.service.notification.StatusBarNotification, android.service.notification.NotificationListenerService.RankingMap) callback for the notification.

Parameters
key String: The key of the notification to snooze

Protected methods

attachBaseContext

protected 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.