NotificationManagerCompat
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 |
|
|---|---|
const String! |
ACTION_BIND_SIDE_CHANNEL = "android.support.BIND_NOTIFICATION_SIDE_CHANNEL"Intent action to register for on a service to receive side channel notifications. |
const 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. |
const Int |
Default notification importance: shows everywhere, allowed to makes noise, but does not visually intrude. |
const Int |
IMPORTANCE_HIGH = 4Higher notification importance: shows everywhere, allowed to makes noise and peek. |
const Int |
IMPORTANCE_LOW = 2Low notification importance: shows everywhere, but is not intrusive. |
const Int |
IMPORTANCE_MAX = 5Highest notification importance: shows everywhere, allowed to makes noise, peek, and use full screen intents. |
const Int |
IMPORTANCE_MIN = 1Min notification importance: only shows in the shade, below the fold. |
const Int |
IMPORTANCE_NONE = 0A notification with no importance: shows nowhere, is blocked. |
const Int |
IMPORTANCE_UNSPECIFIED = -1000Value signifying that the user has not expressed an importance. |
const Int |
|
const Int |
|
const Int |
|
const Int |
|
const Int |
|
Public functions |
|
|---|---|
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. |
Unit |
Cancel a previously shown notification. |
Unit |
Cancel a previously shown notification. |
Unit |
Cancel all previously shown notifications. |
Unit |
createNotificationChannel(channel: NotificationChannel)Creates a notification channel that notifications can be posted to. |
Unit |
Creates a notification channel that notifications can be posted to. |
Unit |
Creates a group container for |
Unit |
Creates a group container for |
Unit |
Creates multiple notification channel groups. |
Unit |
Creates multiple notification channel groups. |
Unit |
createNotificationChannels(Creates multiple notification channels that different notifications can be posted to. |
Unit |
createNotificationChannelsCompat(Creates multiple notification channels that different notifications can be posted to. |
Unit |
deleteNotificationChannel(channelId: String)Deletes the given notification channel. |
Unit |
deleteNotificationChannelGroup(groupId: String)Deletes the given notification channel group, and all notification channels that belong to it. |
Unit |
deleteUnlistedNotificationChannels(Deletes notification channels for which ids are NOT given. |
java-static NotificationManagerCompat |
Get a |
(Mutable)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 |
Int |
Gets the current notification interruption filter. |
java-static (Mutable)Set<String!> |
getEnabledListenerPackages(context: Context)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. |
NotificationChannel? |
getNotificationChannel(channelId: String)Returns the notification channel settings for a given channel id. |
NotificationChannel? |
getNotificationChannel(channelId: String, conversationId: String)Returns the notification channel settings for a given channel and |
NotificationChannelCompat? |
getNotificationChannelCompat(channelId: String)Returns the notification channel settings for a given channel id. |
NotificationChannelCompat? |
getNotificationChannelCompat(channelId: String, conversationId: String)Returns the notification channel settings for a given channel and |
NotificationChannelGroup? |
getNotificationChannelGroup(channelGroupId: String)Returns the notification channel group settings for a given channel group id. |
NotificationChannelGroupCompat? |
getNotificationChannelGroupCompat(channelGroupId: String)Returns the notification channel group settings for a given channel group id. |
(Mutable)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. |
(Mutable)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. |
(Mutable)List<NotificationChannel!> |
Returns all notification channels belonging to the calling app or an empty list on older SDKs which don't support Notification Channels. |
(Mutable)List<NotificationChannelCompat!> |
Returns all notification channels belonging to the calling app or an empty list on older SDKs which don't support Notification Channels. |
Unit |
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)Post a number of notifications, to be shown in the status bar, stream, etc. |
Unit |
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)Post a notification to be shown in the status bar, stream, etc. |
Unit |
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)Post a notification to be shown in the status bar, stream, etc. |
Constants
ACTION_BIND_SIDE_CHANNEL
const val ACTION_BIND_SIDE_CHANNEL = "android.support.BIND_NOTIFICATION_SIDE_CHANNEL": String!
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
const val EXTRA_USE_SIDE_CHANNEL = "android.support.useSideChannel": String!
Notification extras key: if set to true, the posted notification should use the side channel for delivery instead of using notification manager.
IMPORTANCE_DEFAULT
const val IMPORTANCE_DEFAULT = 3: Int
Default notification importance: shows everywhere, allowed to makes noise, but does not visually intrude.
IMPORTANCE_HIGH
const val IMPORTANCE_HIGH = 4: Int
Higher notification importance: shows everywhere, allowed to makes noise and peek.
IMPORTANCE_LOW
const val IMPORTANCE_LOW = 2: Int
Low notification importance: shows everywhere, but is not intrusive.
IMPORTANCE_MAX
const val IMPORTANCE_MAX = 5: Int
Highest notification importance: shows everywhere, allowed to makes noise, peek, and use full screen intents.
IMPORTANCE_MIN
const val IMPORTANCE_MIN = 1: Int
Min notification importance: only shows in the shade, below the fold.
IMPORTANCE_NONE
const val IMPORTANCE_NONE = 0: Int
A notification with no importance: shows nowhere, is blocked.
IMPORTANCE_UNSPECIFIED
const val IMPORTANCE_UNSPECIFIED = -1000: Int
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
const val INTERRUPTION_FILTER_ALARMS = 4: Int
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
const val INTERRUPTION_FILTER_ALL = 1: Int
Interruption filter constant - Normal interruption filter - no notifications are suppressed.
INTERRUPTION_FILTER_NONE
const val INTERRUPTION_FILTER_NONE = 3: 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.
INTERRUPTION_FILTER_PRIORITY
const val INTERRUPTION_FILTER_PRIORITY = 2: Int
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
const val INTERRUPTION_FILTER_UNKNOWN = 0: Int
Interruption filter constant - returned when the value is unavailable for any reason.
Public functions
areNotificationsEnabled
fun areNotificationsEnabled(): Boolean
Returns whether notifications from the calling package are not blocked.
canPostPromotedNotifications
fun canPostPromotedNotifications(): 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. Apps can request this permission by sending the user to the activity that matches the system intent action ACTION_APP_NOTIFICATION_PROMOTION_SETTINGS.
canUseFullScreenIntent
fun canUseFullScreenIntent(): Boolean
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
fun cancel(id: Int): Unit
Cancel a previously shown notification.
| Parameters | |
|---|---|
id: Int |
the ID of the notification |
cancel
fun cancel(tag: String?, id: Int): Unit
Cancel a previously shown notification.
createNotificationChannel
fun createNotificationChannel(channel: NotificationChannel): Unit
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 | |
|---|---|
channel: NotificationChannel |
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
fun createNotificationChannel(channel: NotificationChannelCompat): Unit
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 | |
|---|---|
channel: NotificationChannelCompat |
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
fun createNotificationChannelGroup(group: NotificationChannelGroup): Unit
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 | |
|---|---|
group: NotificationChannelGroup |
The group to create |
createNotificationChannelGroup
fun createNotificationChannelGroup(group: NotificationChannelGroupCompat): Unit
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 | |
|---|---|
group: NotificationChannelGroupCompat |
The group to create |
createNotificationChannelGroups
fun createNotificationChannelGroups(
groups: (Mutable)List<NotificationChannelGroup!>
): Unit
Creates multiple notification channel groups. See createNotificationChannelGroup. It doesn't do anything on older SDKs which don't support Notification Channels.
| Parameters | |
|---|---|
groups: (Mutable)List<NotificationChannelGroup!> |
The list of groups to create |
createNotificationChannelGroupsCompat
fun createNotificationChannelGroupsCompat(
groups: (Mutable)List<NotificationChannelGroupCompat!>
): Unit
Creates multiple notification channel groups. See createNotificationChannelGroup. It doesn't do anything on older SDKs which don't support Notification Channels.
| Parameters | |
|---|---|
groups: (Mutable)List<NotificationChannelGroupCompat!> |
The list of groups to create |
createNotificationChannels
fun createNotificationChannels(
channels: (Mutable)List<NotificationChannel!>
): Unit
Creates multiple notification channels that different notifications can be posted to. See createNotificationChannel. It doesn't do anything on older SDKs which don't support Notification Channels.
| Parameters | |
|---|---|
channels: (Mutable)List<NotificationChannel!> |
the list of channels to attempt to create. |
createNotificationChannelsCompat
fun createNotificationChannelsCompat(
channels: (Mutable)List<NotificationChannelCompat!>
): Unit
Creates multiple notification channels that different notifications can be posted to. See createNotificationChannel. It doesn't do anything on older SDKs which don't support Notification Channels.
| Parameters | |
|---|---|
channels: (Mutable)List<NotificationChannelCompat!> |
the list of channels to attempt to create. |
deleteNotificationChannel
fun deleteNotificationChannel(channelId: String): Unit
Deletes the given notification channel.
If you create a new channel with this same id, the deleted channel will be un-deleted with all of the same settings it had before it was deleted. It doesn't do anything on older SDKs which don't support Notification Channels.
deleteNotificationChannelGroup
fun deleteNotificationChannelGroup(groupId: String): Unit
Deletes the given notification channel group, and all notification channels that belong to it. It doesn't do anything on older SDKs which don't support Notification Channels.
deleteUnlistedNotificationChannels
fun deleteUnlistedNotificationChannels(
channelIds: (Mutable)Collection<String!>
): Unit
Deletes notification channels for which ids are NOT given. This will NOT delete channels which are conversation children of the given channels. It doesn't do anything on older SDKs which don't support Notification Channels.
| Parameters | |
|---|---|
channelIds: (Mutable)Collection<String!> |
the IDs of any channels which should NOT be deleted by this method. |
from
java-static fun from(context: Context): NotificationManagerCompat
Get a NotificationManagerCompat instance for a provided context.
getActiveNotifications
fun getActiveNotifications(): (Mutable)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.
StatusBarNotification object, including the original and supplied to (via and ) as well as a copy of the original object (via ).
From Q, will also return notifications you've posted as an app's notification delegate via notifyAsPackage.
Returns an empty list on LOLLIPOP_MR1 and earlier.
| Returns | |
|---|---|
(Mutable)List<StatusBarNotification!> |
A list of |
getCurrentInterruptionFilter
fun getCurrentInterruptionFilter(): Int
Gets the current notification interruption filter.
The interruption filter defines which notifications are allowed to interrupt the user (e.g. via sound &vibration) and is applied globally.
getEnabledListenerPackages
java-static fun getEnabledListenerPackages(context: Context): (Mutable)Set<String!>
Get the set of packages that have an enabled notification listener component within them.
getImportance
fun getImportance(): Int
Returns the user specified importance for notifications from the calling package.
| Returns | |
|---|---|
Int |
An importance level, such as |
getNotificationChannel
fun getNotificationChannel(channelId: String): NotificationChannel?
Returns the notification channel settings for a given channel id. Returns null on older SDKs which don't support Notification Channels.
getNotificationChannel
fun getNotificationChannel(channelId: String, conversationId: String): NotificationChannel?
Returns the notification channel settings for a given channel and conversation id. Returns the channel for the channelId on older SDKs which don't support Conversations. Returns null on older SDKs which don't support Notification Channels.
getNotificationChannelCompat
fun getNotificationChannelCompat(channelId: String): NotificationChannelCompat?
Returns the notification channel settings for a given channel id. Returns null on older SDKs which don't support Notification Channels.
getNotificationChannelCompat
fun getNotificationChannelCompat(channelId: String, conversationId: String): NotificationChannelCompat?
Returns the notification channel settings for a given channel and conversation id. Returns the channel for the channelId on older SDKs which don't support Conversations. Returns null on older SDKs which don't support Notification Channels.
getNotificationChannelGroup
fun getNotificationChannelGroup(channelGroupId: String): NotificationChannelGroup?
Returns the notification channel group settings for a given channel group id. Returns null on older SDKs which don't support Notification Channels.
getNotificationChannelGroupCompat
fun getNotificationChannelGroupCompat(channelGroupId: String): NotificationChannelGroupCompat?
Returns the notification channel group settings for a given channel group id. Returns null on older SDKs which don't support Notification Channels.
getNotificationChannelGroups
fun getNotificationChannelGroups(): (Mutable)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.
getNotificationChannelGroupsCompat
fun getNotificationChannelGroupsCompat(): (Mutable)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.
getNotificationChannels
fun getNotificationChannels(): (Mutable)List<NotificationChannel!>
Returns all notification channels belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.
getNotificationChannelsCompat
fun getNotificationChannelsCompat(): (Mutable)List<NotificationChannelCompat!>
Returns all notification channels belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.
notify
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)
fun notify(
notificationWithIdAndTags: (Mutable)List<NotificationManagerCompat.NotificationWithIdAndTag!>
): Unit
Post a number of notifications, to be shown in the status bar, stream, etc. Each notification will attempt to be posted in the order provided in the
notificationWithIds list. Each notification must have a provided id and may have a provided tag. This is the preferred method for posting groups of notifications, to improve sound and animation behavior.
notify
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)
fun notify(id: Int, notification: Notification): Unit
Post a notification to be shown in the status bar, stream, etc.
| Parameters | |
|---|---|
id: Int |
the ID of the notification |
notification: Notification |
the notification to post to the system |
notify
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)
fun notify(tag: String?, id: Int, notification: Notification): Unit
Post a notification to be shown in the status bar, stream, etc.
| Parameters | |
|---|---|
tag: String? |
the string identifier for a notification. Can be |
id: Int |
the ID of the notification. The pair (tag, id) must be unique within your app. |
notification: Notification |
the notification to post to the system |