FileDocCategorySizeDatePackage
NotificationManagerCompat.javaAPI DocAndroid 5.1 API25046Thu Mar 12 22:22:56 GMT 2015android.support.v4.app

NotificationManagerCompat

public class NotificationManagerCompat extends Object
Compatibility library for NotificationManager with fallbacks for older platforms.

To use this class, call the static function {@link #from} to get a {@link NotificationManagerCompat} object, and then call one of its methods to post or cancel notifications.

Fields Summary
private static final String
TAG
public static final String
EXTRA_USE_SIDE_CHANNEL
Notification extras key: if set to true, the posted notification should use the side channel for delivery instead of using notification manager.
public static final String
ACTION_BIND_SIDE_CHANNEL
Intent action to register for on a service to receive side channel notifications. The listening service must be in the same package as an enabled {@link android.service.notification.NotificationListenerService}.
static final int
MAX_SIDE_CHANNEL_SDK_VERSION
Maximum sdk build version which needs support for side channeled notifications. Currently the only needed use is for side channeling group children before KITKAT_WATCH.
private static final int
SIDE_CHANNEL_RETRY_BASE_INTERVAL_MS
Base time delay for a side channel listener queue retry.
private static final int
SIDE_CHANNEL_RETRY_MAX_COUNT
Maximum retries for a side channel listener before dropping tasks.
private static final String
SETTING_ENABLED_NOTIFICATION_LISTENERS
Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS
private static final int
SIDE_CHANNEL_BIND_FLAGS
private static final Object
sEnabledNotificationListenersLock
Cache of enabled notification listener components
private static String
sEnabledNotificationListeners
Guarded by {@link #sEnabledNotificationListenersLock}
private static Set
sEnabledNotificationListenerPackages
Guarded by {@link #sEnabledNotificationListenersLock}
private final android.content.Context
mContext
private final android.app.NotificationManager
mNotificationManager
private static final Object
sLock
Lock for mutable static fields
private static SideChannelManager
sSideChannelManager
Guarded by {@link #sLock}
private static final Impl
IMPL
Constructors Summary
private NotificationManagerCompat(android.content.Context context)

        mContext = context;
        mNotificationManager = (NotificationManager) mContext.getSystemService(
                Context.NOTIFICATION_SERVICE);
    
Methods Summary
public voidcancel(int id)
Cancel a previously shown notification.

param
id the ID of the notification

        if (Build.VERSION.SDK_INT >= 14) {
            IMPL = new ImplIceCreamSandwich();
        } else if (Build.VERSION.SDK_INT >= 5) {
            IMPL = new ImplEclair();
        } else {
            IMPL = new ImplBase();
        }
        SIDE_CHANNEL_BIND_FLAGS = IMPL.getSideChannelBindFlags();
    
        cancel(null, id);
    
public voidcancel(java.lang.String tag, int id)
Cancel a previously shown notification.

param
tag the string identifier of the notification.
param
id the ID of the notification

        IMPL.cancelNotification(mNotificationManager, tag, id);
        if (Build.VERSION.SDK_INT <= MAX_SIDE_CHANNEL_SDK_VERSION) {
            pushSideChannelQueue(new CancelTask(mContext.getPackageName(), id, tag));
        }
    
public voidcancelAll()
Cancel all previously shown notifications.

        mNotificationManager.cancelAll();
        if (Build.VERSION.SDK_INT <= MAX_SIDE_CHANNEL_SDK_VERSION) {
            pushSideChannelQueue(new CancelTask(mContext.getPackageName()));
        }
    
public static android.support.v4.app.NotificationManagerCompatfrom(android.content.Context context)
Get a {@link NotificationManagerCompat} instance for a provided context.


              
         
        return new NotificationManagerCompat(context);
    
public static java.util.SetgetEnabledListenerPackages(android.content.Context context)
Get the set of packages that have an enabled notification listener component within them.

        final String enabledNotificationListeners = Settings.Secure.getString(
                context.getContentResolver(),
                SETTING_ENABLED_NOTIFICATION_LISTENERS);
        // Parse the string again if it is different from the last time this method was called.
        if (enabledNotificationListeners != null
                && !enabledNotificationListeners.equals(sEnabledNotificationListeners)) {
            final String[] components = enabledNotificationListeners.split(":");
            Set<String> packageNames = new HashSet<String>(components.length);
            for (String component : components) {
                ComponentName componentName = ComponentName.unflattenFromString(component);
                if (componentName != null) {
                    packageNames.add(componentName.getPackageName());
                }
            }
            synchronized (sEnabledNotificationListenersLock) {
                sEnabledNotificationListenerPackages = packageNames;
                sEnabledNotificationListeners = enabledNotificationListeners;
            }
        }
        return sEnabledNotificationListenerPackages;
    
public voidnotify(int id, android.app.Notification notification)
Post a notification to be shown in the status bar, stream, etc.

param
id the ID of the notification
param
notification the notification to post to the system

        notify(null, id, notification);
    
public voidnotify(java.lang.String tag, int id, android.app.Notification notification)
Post a notification to be shown in the status bar, stream, etc.

param
tag the string identifier for a notification. Can be {@code null}.
param
id the ID of the notification. The pair (tag, id) must be unique within your app.
param
notification the notification to post to the system

        if (useSideChannelForNotification(notification)) {
            pushSideChannelQueue(new NotifyTask(mContext.getPackageName(), id, tag, notification));
            // Cancel this notification in notification manager if it just transitioned to being
            // side channelled.
            IMPL.cancelNotification(mNotificationManager, tag, id);
        } else {
            IMPL.postNotification(mNotificationManager, tag, id, notification);
        }
    
private voidpushSideChannelQueue(android.support.v4.app.NotificationManagerCompat$Task task)
Push a notification task for distribution to notification side channels.

        synchronized (sLock) {
            if (sSideChannelManager == null) {
                sSideChannelManager = new SideChannelManager(mContext.getApplicationContext());
            }
        }
        sSideChannelManager.queueTask(task);
    
private static booleanuseSideChannelForNotification(android.app.Notification notification)
Returns true if this notification should use the side channel for delivery.

        Bundle extras = NotificationCompat.getExtras(notification);
        return extras != null && extras.getBoolean(EXTRA_USE_SIDE_CHANNEL);