NotificationManagerCompat

Added in 1.1.0

public final class NotificationManagerCompat


Compatibility library for NotificationManager with fallbacks for older platforms.

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

Summary

Nested types

Helper class which encapsulates a Notification, its id, and optionally a tag, for use when batch-posting a number of notifications.

Constants

static final String
ACTION_BIND_SIDE_CHANNEL = "android.support.BIND_NOTIFICATION_SIDE_CHANNEL"

Intent action to register for on a service to receive side channel notifications.

static final String
EXTRA_USE_SIDE_CHANNEL = "android.support.useSideChannel"

Notification extras key: if set to true, the posted notification should use the side channel for delivery instead of using notification manager.

static final int

Default notification importance: shows everywhere, allowed to makes noise, but does not visually intrude.

static final int

Higher notification importance: shows everywhere, allowed to makes noise and peek.

static final int

Low notification importance: shows everywhere, but is not intrusive.

static final int

Highest notification importance: shows everywhere, allowed to makes noise, peek, and use full screen intents.

static final int

Min notification importance: only shows in the shade, below the fold.

static final int

A notification with no importance: shows nowhere, is blocked.

static final int

Value signifying that the user has not expressed an importance.

static final int

Interruption filter constant - Alarms only interruption filter - all notifications except those of category CATEGORY_ALARM are suppressed.

static final int

Interruption filter constant - Normal interruption filter - no notifications are suppressed.

static final int

Interruption filter constant - No interruptions filter - all notifications are suppressed and all audio streams (except those used for phone calls) and vibrations are muted.

static final int

Interruption filter constant - Priority interruption filter - all notifications are suppressed except those that match the priority criteria.

static final int

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

Public methods

boolean

Returns whether notifications from the calling package are not blocked.

boolean

Returns whether the calling app's properly formatted notifications can appear in a promoted format, which may result in higher ranking, appearances on additional surfaces, and richer presentation.

boolean

Returns whether the calling app can send fullscreen intents.

void
cancel(int id)

Cancel a previously shown notification.

void
cancel(@Nullable String tag, int id)

Cancel a previously shown notification.

void

Cancel all previously shown notifications.

void

Creates a notification channel that notifications can be posted to.

void

Creates a notification channel that notifications can be posted to.

void

Creates a group container for NotificationChannel objects.

void

Creates a group container for NotificationChannel objects.

void

Creates multiple notification channel groups.

void

Creates multiple notification channel groups.

void

Creates multiple notification channels that different notifications can be posted to.

void

Creates multiple notification channels that different notifications can be posted to.

void

Deletes the given notification channel.

void

Deletes the given notification channel group, and all notification channels that belong to it.

void

Deletes notification channels for which ids are NOT given.

static @NonNull NotificationManagerCompat
from(@NonNull Context context)

Get a NotificationManagerCompat instance for a provided context.

@NonNull List<StatusBarNotification>

Recover a list of active notifications: ones that have been posted by the calling app that have not yet been dismissed by the user or canceled by the app.

int

Gets the current notification interruption filter.

static @NonNull Set<String>

Get the set of packages that have an enabled notification listener component within them.

int

Returns the user specified importance for notifications from the calling package.

@Nullable NotificationChannel

Returns the notification channel settings for a given channel id.

@Nullable NotificationChannel
getNotificationChannel(
    @NonNull String channelId,
    @NonNull String conversationId
)

Returns the notification channel settings for a given channel and conversation id.

@Nullable NotificationChannelCompat

Returns the notification channel settings for a given channel id.

@Nullable NotificationChannelCompat
getNotificationChannelCompat(
    @NonNull String channelId,
    @NonNull String conversationId
)

Returns the notification channel settings for a given channel and conversation id.

@Nullable NotificationChannelGroup

Returns the notification channel group settings for a given channel group id.

@Nullable NotificationChannelGroupCompat

Returns the notification channel group settings for a given channel group id.

@NonNull List<NotificationChannelGroup>

Returns all notification channel groups belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.

@NonNull List<NotificationChannelGroupCompat>

Returns all notification channel groups belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.

@NonNull List<NotificationChannel>

Returns all notification channels belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.

@NonNull List<NotificationChannelCompat>

Returns all notification channels belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.

void

Post a number of notifications, to be shown in the status bar, stream, etc.

void

Post a notification to be shown in the status bar, stream, etc.

void

Post a notification to be shown in the status bar, stream, etc.

Constants

ACTION_BIND_SIDE_CHANNEL

Added in 1.1.0
public static final String ACTION_BIND_SIDE_CHANNEL = "android.support.BIND_NOTIFICATION_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 NotificationListenerService.

EXTRA_USE_SIDE_CHANNEL

Added in 1.1.0
public static final String EXTRA_USE_SIDE_CHANNEL = "android.support.useSideChannel"

Notification extras key: if set to true, the posted notification should use the side channel for delivery instead of using notification manager.

IMPORTANCE_DEFAULT

Added in 1.1.0
public static final int IMPORTANCE_DEFAULT = 3

Default notification importance: shows everywhere, allowed to makes noise, but does not visually intrude.

IMPORTANCE_HIGH

Added in 1.1.0
public static final int IMPORTANCE_HIGH = 4

Higher notification importance: shows everywhere, allowed to makes noise and peek.

IMPORTANCE_LOW

Added in 1.1.0
public static final int IMPORTANCE_LOW = 2

Low notification importance: shows everywhere, but is not intrusive.

IMPORTANCE_MAX

Added in 1.1.0
public static final int IMPORTANCE_MAX = 5

Highest notification importance: shows everywhere, allowed to makes noise, peek, and use full screen intents.

IMPORTANCE_MIN

Added in 1.1.0
public static final int IMPORTANCE_MIN = 1

Min notification importance: only shows in the shade, below the fold.

IMPORTANCE_NONE

Added in 1.1.0
public static final int IMPORTANCE_NONE = 0

A notification with no importance: shows nowhere, is blocked.

IMPORTANCE_UNSPECIFIED

Added in 1.1.0
public static final int IMPORTANCE_UNSPECIFIED = -1000

Value signifying that the user has not expressed an importance. This value is for persisting preferences, and should never be associated with an actual notification.

INTERRUPTION_FILTER_ALARMS

Added in 1.11.0
public static final int INTERRUPTION_FILTER_ALARMS = 4

Interruption filter constant - Alarms only interruption filter - all notifications except those of category CATEGORY_ALARM are suppressed. Some audio streams are muted.

INTERRUPTION_FILTER_ALL

Added in 1.11.0
public static final int INTERRUPTION_FILTER_ALL = 1

Interruption filter constant - Normal interruption filter - no notifications are suppressed.

INTERRUPTION_FILTER_NONE

Added in 1.11.0
public static final int INTERRUPTION_FILTER_NONE = 3

Interruption filter constant - No interruptions filter - all notifications are suppressed and all audio streams (except those used for phone calls) and vibrations are muted.

INTERRUPTION_FILTER_PRIORITY

Added in 1.11.0
public static final int INTERRUPTION_FILTER_PRIORITY = 2

Interruption filter constant - Priority interruption filter - all notifications are suppressed except those that match the priority criteria. Some audio streams are muted. See priorityCallSenders, priorityCategories, priorityMessageSenders to define or query this criteria. Users can additionally specify packages that can bypass this interruption filter.

INTERRUPTION_FILTER_UNKNOWN

Added in 1.11.0
public static final int INTERRUPTION_FILTER_UNKNOWN = 0

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

Public methods

areNotificationsEnabled

Added in 1.1.0
public boolean areNotificationsEnabled()

Returns whether notifications from the calling package are not blocked.

canPostPromotedNotifications

Added in 1.17.0-beta01
public boolean canPostPromotedNotifications()

Returns whether the calling app's properly formatted notifications can appear in a promoted format, which may result in higher ranking, appearances on additional surfaces, and richer presentation. Apps can request this permission by sending the user to the activity that matches the system intent action ACTION_APP_NOTIFICATION_PROMOTION_SETTINGS.

canUseFullScreenIntent

Added in 1.12.0
public boolean canUseFullScreenIntent()

Returns whether the calling app can send fullscreen intents.

Fullscreen intents were introduced in Android HONEYCOMB, where apps could always attach a full screen intent to their notification via setFullScreenIntent}.

Android Q introduced the USE_FULL_SCREEN_INTENT permission, where SystemUI will only show the full screen intent attached to a notification if the permission is declared in the manifest.

Starting from Android UPSIDE_DOWN_CAKE, apps may not have permission to use USE_FULL_SCREEN_INTENT. If the FSI permission is denied, SystemUI will show the notification as an expanded heads up notification on lockscreen.

To request access, add the USE_FULL_SCREEN_INTENT permission to your manifest, and use ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT to send the user to the settings page where they can grant your app the FSI permission.

cancel

Added in 1.1.0
public void cancel(int id)

Cancel a previously shown notification.

Parameters
int id

the ID of the notification

cancel

Added in 1.1.0
public void cancel(@Nullable String tag, int id)

Cancel a previously shown notification.

Parameters
@Nullable String tag

the string identifier of the notification.

int id

the ID of the notification

cancelAll

Added in 1.1.0
public void cancelAll()

Cancel all previously shown notifications.

createNotificationChannel

Added in 1.1.0
public void createNotificationChannel(@NonNull NotificationChannel channel)

Creates a notification channel that notifications can be posted to. This can also be used to restore a deleted channel and to update an existing channel's name, description, group, and/or importance.

The importance of an existing channel will only be changed if the new importance is lower than the current value and the user has not altered any settings on this channel.

The group of an existing channel will only be changed if the channel does not already belong to a group. All other fields are ignored for channels that already exist. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull NotificationChannel channel

the channel to create. Note that the created channel may differ from this value. If the provided channel is malformed, a RemoteException will be thrown.

createNotificationChannel

Added in 1.5.0
public void createNotificationChannel(@NonNull NotificationChannelCompat channel)

Creates a notification channel that notifications can be posted to. This can also be used to restore a deleted channel and to update an existing channel's name, description, group, and/or importance.

The importance of an existing channel will only be changed if the new importance is lower than the current value and the user has not altered any settings on this channel.

The group of an existing channel will only be changed if the channel does not already belong to a group. All other fields are ignored for channels that already exist. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull NotificationChannelCompat channel

the channel to create. Note that the created channel may differ from this value. If the provided channel is malformed, a RemoteException will be thrown.

createNotificationChannelGroup

Added in 1.1.0
public void createNotificationChannelGroup(@NonNull NotificationChannelGroup group)

Creates a group container for NotificationChannel objects. This can be used to rename an existing group. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull NotificationChannelGroup group

The group to create

createNotificationChannelGroup