Class NotificationChannelBuilder
Builds and registers a notification channel. Notification channels are an Android concept (introduced in Android O) that let the user control the behavior of groups of notifications: importance, sound, vibration, lights, lockscreen visibility and whether a badge is shown. Once a channel is created its user-controllable settings cannot be changed programmatically, so build it once at app startup.
On platforms without a channel concept (iOS, desktop) registering a channel is a
no-op, but the channel id you assign to a LocalNotification is still carried so the
notification behaves consistently.
Usage
new NotificationChannelBuilder("messages", "Messages")
.description("Incoming chat messages")
.importance(NotificationChannelBuilder.IMPORTANCE_HIGH)
.sound("/notification_sound_ping.mp3")
.enableVibration(true)
.register();
See also
- LocalNotification#setChannelId(String)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intChannel importance: shows everywhere, makes noise but does not visually intrude.static final intChannel importance: makes noise and shows as a heads-up notification.static final intChannel importance: shows in the shade and status bar but is not intrusive.static final intChannel importance: the highest level (rarely needed).static final intChannel importance: shows nowhere, is not intrusive.static final intChannel importance: a no-importance channel does not appear in the shade.static final intLockscreen visibility: show the notification but hide sensitive content on a secure lockscreen.static final intLockscreen visibility: show the notification in its entirety on the lockscreen.static final intLockscreen visibility: do not reveal any part of the notification on a secure lockscreen. -
Constructor Summary
ConstructorsConstructorDescriptionNotificationChannelBuilder(String id, String name) Creates a channel builder. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcreateChannelGroup(String groupId, String groupName) Creates a channel group, which visually groups channels in the system settings.static voiddeleteChannel(String id) Deletes a previously registered channel.Sets the user-visible channel description.enableLights(boolean b) Enables or disables the notification light for this channel.enableVibration(boolean b) Enables or disables vibration for this channel.Returns the channel description.getGroup()Returns the channel group id.getId()Returns the channel id.intReturns the channel importance.intReturns the light color.intReturns the lockscreen visibility.getName()Returns the user-visible channel name.getSound()Returns the channel sound file path.long[]Returns the vibration pattern.Assigns this channel to a channel group.importance(int imp) Sets the channel importance, one of theIMPORTANCE_constants.booleanReturns true if lights are enabled.booleanReturns true if a launcher badge is allowed.booleanReturns true if vibration is enabled.lightColor(int rgb) Sets the notification light color (as an RGB integer) and enables lights.lockscreenVisibility(int v) Sets the lockscreen visibility, one of theVISIBILITY_constants.voidregister()Registers this channel with the platform.showBadge(boolean b) Controls whether notifications on this channel may show a launcher badge.Sets the sound played for notifications on this channel.vibrationPattern(long[] pattern) Sets the vibration pattern (alternating off/on durations in milliseconds) and enables vibration.
-
Field Details
-
IMPORTANCE_NONE
public static final int IMPORTANCE_NONEChannel importance: a no-importance channel does not appear in the shade.- See Also:
-
IMPORTANCE_MIN
public static final int IMPORTANCE_MINChannel importance: shows nowhere, is not intrusive.- See Also:
-
IMPORTANCE_LOW
public static final int IMPORTANCE_LOWChannel importance: shows in the shade and status bar but is not intrusive.- See Also:
-
IMPORTANCE_DEFAULT
public static final int IMPORTANCE_DEFAULTChannel importance: shows everywhere, makes noise but does not visually intrude.- See Also:
-
IMPORTANCE_HIGH
public static final int IMPORTANCE_HIGHChannel importance: makes noise and shows as a heads-up notification.- See Also:
-
IMPORTANCE_MAX
public static final int IMPORTANCE_MAXChannel importance: the highest level (rarely needed).- See Also:
-
VISIBILITY_SECRET
public static final int VISIBILITY_SECRETLockscreen visibility: do not reveal any part of the notification on a secure lockscreen.- See Also:
-
VISIBILITY_PRIVATE
public static final int VISIBILITY_PRIVATELockscreen visibility: show the notification but hide sensitive content on a secure lockscreen.- See Also:
-
VISIBILITY_PUBLIC
public static final int VISIBILITY_PUBLICLockscreen visibility: show the notification in its entirety on the lockscreen.- See Also:
-
-
Constructor Details
-
NotificationChannelBuilder
-
-
Method Details
-
description
Sets the user-visible channel description.
Parameters
d: the description
Returns
this builder for chaining
-
importance
Sets the channel importance, one of the
IMPORTANCE_constants.Parameters
imp: the importance level
Returns
this builder for chaining
-
sound
Sets the sound played for notifications on this channel. The file name must start with the "notification_sound" prefix and be bundled with the app.
Parameters
soundFile: the sound file path
Returns
this builder for chaining
-
enableVibration
Enables or disables vibration for this channel.
Parameters
b: true to enable vibration
Returns
this builder for chaining
-
vibrationPattern
Sets the vibration pattern (alternating off/on durations in milliseconds) and enables vibration.
Parameters
pattern: the vibration pattern
Returns
this builder for chaining
-
enableLights
Enables or disables the notification light for this channel.
Parameters
b: true to enable lights
Returns
this builder for chaining
-
lightColor
Sets the notification light color (as an RGB integer) and enables lights.
Parameters
rgb: the light color
Returns
this builder for chaining
-
lockscreenVisibility
Sets the lockscreen visibility, one of the
VISIBILITY_constants.Parameters
v: the lockscreen visibility
Returns
this builder for chaining
-
group
Assigns this channel to a channel group. The group must be created with
#createChannelGroup(String, String)before or after the channel is registered.Parameters
groupId: the channel group id
Returns
this builder for chaining
-
showBadge
Controls whether notifications on this channel may show a launcher badge.
Parameters
b: true to allow a badge
Returns
this builder for chaining
-
getId
Returns the channel id.
Returns
the channel id
-
getName
Returns the user-visible channel name.
Returns
the channel name
-
getDescription
Returns the channel description.
Returns
the description, or null
-
getImportance
public int getImportance()Returns the channel importance.
Returns
the importance level
-
getSound
Returns the channel sound file path.
Returns
the sound file, or null
-
isVibrationEnabled
public boolean isVibrationEnabled()Returns true if vibration is enabled.
Returns
true if vibration is enabled
-
getVibrationPattern
public long[] getVibrationPattern()Returns the vibration pattern.
Returns
the vibration pattern, or null
-
isLightsEnabled
public boolean isLightsEnabled()Returns true if lights are enabled.
Returns
true if lights are enabled
-
getLightColor
public int getLightColor()Returns the light color.
Returns
the light color as an RGB integer
-
getLockscreenVisibility
public int getLockscreenVisibility()Returns the lockscreen visibility.
Returns
the lockscreen visibility
-
getGroup
Returns the channel group id.
Returns
the group id, or null
-
isShowBadge
public boolean isShowBadge()Returns true if a launcher badge is allowed.
Returns
true if a badge is allowed
-
register
public void register()Registers this channel with the platform. On platforms without channels this is a no-op. -
deleteChannel
Deletes a previously registered channel. On platforms without channels this is a no-op.
Parameters
id: the channel id to delete
-
createChannelGroup
-