Android Code Snippet Custom notifications channel


Staff member
Licensed User
Starting from Android 8, notifications are grouped by channels. The notification behavior depends on the channel it belongs to.
With B4A v8+ notification channels are created automatically based on the importance level.

You can use the following code to manually create a notification channel with the same id as the notification channel that will be created internally. This allows you to override the notification behavior.

Note that once a notification channel was created then you cannot change its behavior. This means that:

1. You must call this code before you initialize the notification object.
2. If you have already created a notification in the past then you need to uninstall the app to see the changes (clean the project afterward).

Sub CreateNotificationChannel(ImportanceLevel As Int)
   Dim p As Phone
   If p.SdkVersion >= 26 Then
       Dim ctxt As JavaObject
       Dim channelId As String = "channel_" & ImportanceLevel
       Dim channel As JavaObject
       channel.InitializeNewInstance("", Array(channelId, Application.LabelName, ImportanceLevel))
       'modify the channel
       'For example: disable the badge feature
       channel.RunMethod("setShowBadge", Array(False))
       'set it
       Dim manager As JavaObject = ctxt.RunMethod("getSystemService", Array("notification"))
       manager.RunMethod("createNotificationChannel", Array(channel))
   End If
End Sub
Dim n As Notification
   n.Icon = "icon"
   n.SetInfo("test", "test", Main)
Depends on: Phone and JavaObject libraries. Requires B4A v8+