Android Notification Channels
With the introduction of Android Oreo, Google has strived to make the Notifications system more user-friendly. Android Oreo has completely redesigned notifications.
Notification Channels are somewhat like groups or categories of notifications - say you’re building a social networking app, the channels can be “activity” - likes, or comments on your posts, “messages”, etc.
To define your channels, take inventory of all the notifications you want to send. Group these notifications into sets that have the following things in common:
Subject matter: A single topic can succinctly describe all of these notifications, such as "Downloads".
Users can get fine control of what they want to be notified about. They can specifically:
A. Turn off notifications for a certain channel
B. Specify the importance
C. Set the preferred sound for a particular category of notifications
Important
After you create a notification channel, you cannot change the notification behaviors — the user has complete control at that point. Though you can still change a channel's name and description.
Note
If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error.
Creating Notification Channels
You can create stand-alone notification channels in your app, by using the following line of code.
NVNotificationChannels.Builder builder1 = new NVNotificationChannels.Builder();
builder1.setChannelID("channel_id_1");
builder1.setChannelName("channel_name_1");
builder1.setImportance(4);
builder1.setChannelDescription("channel_description_1");
builder1.build();
Set<NVNotificationChannels.Builder> nChannelSets = new HashSet<>();
nChannelSets.add(builder1);
NotifyVisitorsApi.getInstance(activityContext).createNotificationChannel(nChannelSets);
val builder1 = NVNotificationChannels.Builder()
builder1.setChannelID("channel_id_1")
builder1.setChannelName("channel_name_1")
builder1.setImportance(4)
builder1.setChannelDescription("channel_description_1")
builder1.build()
val nChannelSets: MutableSet<NVNotificationChannels.Builder> = HashSet()
nChannelSets.add(builder1)
NotifyVisitorsApi.getInstance(activityContext).createNotificationChannel(nChannelSets)
You can create more than one channel using the above line of code, just make sure that the channel ID differs in every Notification Channel. Also, this Channel ID will be used to send Push Notifications using the NotifyVisitors dashboard.
ChannelID
This is the ID used to create different channels. It should be unique. It is not visible to the user. It is mandatory to pass.
builder1.setChannelID("channel_id_1001");
builder1.setChannelID("channel_id_1001")
ChannelName
This is the name of the channel. It should be meaningful. It is visible to the user.
builder1.setChannelName("Miscellaneous");
builder1.setChannelName("Miscellaneous")
ChannelDescription
This description section lets you add more details on what it is to be used for. It is visible to the user.
builder1.setChannelDescription("Notification Drawer,Lock Screen, Ringtone");
builder1.setChannelDescription("Notification Drawer,Lock Screen, Ringtone")
Importance
This configures the level a channel can interrupt a user, ranging from IMPORTANCE_NONE (0) to IMPORTANCE_HIGH (4) or IMPORTANCE_MAX (5). For more details, refer to this.
builder1.setImportance(4);
builder1.setImportance(4)
For values to be passed refer to the table below.
VALUE
|
IMPORTANCE
|
0 |
IMPORTANCE_NONE
|
1 |
IMPORTANCE_MIN
|
2 |
IMPORTANCE_LOW
|
3 |
IMPORTANCE_DEFAULT
|
4 | IMPORTANCE_HIGH
|
5
|
IMPORTANCE_MAX
|
EnableLights
Certain Android hardware devices have LED notification lights built-in, enabling notifications to trigger a colored light on the device upon reception. Via the undermentioned code, you can control whether devices should or should not trigger the aforementioned notification light.
builder1.setEnableLights(true);
builder1.setEnableLights(true)
LightColor
Notification LED colors are set using the Color class (e.g. a green LED notification light would be in the format Color.GREEN). If the LED Color is not set, then the light uses the device's default color, if enabled true.
builder1.setLightColor(Color.GREEN);
builder1.setLightColor(Color.GREEN)
SoundFileName
It sets the sound that should be played for notifications posted to its associated channel.
If the device cannot find the file in an app, or if the file is not in a supported format, it will use the system's default notification sound. The supported formats are as follows: .wav, .mp3 and .ogg. Add your sound file to the app modules’ raw folder i.e., app/src/main/res/raw.
Recommended
Sound filenames should be in lowercase since some platforms ignore uppercase letters for sound files.
For example: Instead of “Arabian-Mystery.wav” use “arabianmystery.wav” or “arabian_mystery.wav”.
Pass the name of the file without extension (e.g. “unconvinced” NOT “unconvinced.wav”).
builder1.setSoundFileName("unconvinced");
builder1.setSoundFileName("unconvinced")
ShouldVibrate
It sets whether a notification posted to its corresponding channel should vibrate the device or not.
builder1.setShouldVibrate(true);
builder1.setShouldVibrate(true)
VibrationPattern
It can set the vibration off, to the phone's default settings, or to any custom vibration pattern.
The first value in the vibration pattern defines the number of milliseconds to wait before turning the device's vibrator on. The next value indicates the number of milliseconds for which the vibrator should be kept on before turning it off and subsequent values alternate between these two. So, in fact pattern sequence means OFF, ON, OFF, ON ... , therefore to have any vibration, you need at least two values.
For example: 0, 400, 700, 300 means pause for 0ms, vibrate for 400ms, then pause for 700ms, then vibrate for 300ms again.
builder1.setVibrationPattern(new long[]{0, 400, 700, 300});
builder1.setVibrationPattern(longArrayOf(0, 400, 700, 300))
Creating Notification Channel Groups
Once you have the NotifyVisitors Android SDK integrated successfully, you can create a notification channel group. Notification channel groups allow you to manage multiple notification channels with identical names within a single app, which can be useful if your app supports multiple accounts. You can create a notification group using the undermentioned code:
NotifyVisitorsApi.getInstance(activityContext).createNotificationChannelGroup(String groupId, CharSequence groupName);
NotifyVisitorsApi.getInstance(activityContext).createNotificationChannelGroup(String groupId, CharSequence groupName)
Note
GroupID & GroupName information are only used for presentation on the user's device, not for behavior.
Refer to the example provided below:
NotifyVisitorsApi.getInstance(activityContext).createNotificationChannelGroup("group56", "Custom Group");
NotifyVisitorsApi.getInstance(activityContext).createNotificationChannelGroup("group56", "Custom Group")
Creating Notification Channels with Notification Groups
Once you have created a notification channel group you can use the group Id to create notification channels for that specific group. You can create a notification channel specifying the group id using the following line of code.
NVNotificationChannels.Builder builder1 = new NVNotificationChannels.Builder();
builder1.setChannelID("channel_id_1");
builder1.setChannelName("channel_name_1");
builder1.setImportance(4);
builder1.setChannelDescription("channel_description_1");
builder1.setEnableLights(true);
builder1.setLightColor(Color.GREEN);
builder1.setSoundFileName("unconvinced");
builder1.setShouldVibrate(true);
builder1.setGroupID("group56");
builder1.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
builder1.build();
Set<NVNotificationChannels.Builder> nChannelSets = new HashSet<>();
nChannelSets.add(builder1);
NotifyVisitorsApi.getInstance(activityContext).createNotificationChannel(nChannelSets);
val builder1 = NVNotificationChannels.Builder()
builder1.setChannelID("channel_id_1")
builder1.setChannelName("channel_name_1")
builder1.setImportance(4)
builder1.setChannelDescription("channel_description_1")
builder1.setEnableLights(true)
builder1.setLightColor(Color.GREEN)
builder1.setSoundFileName("unconvinced")
builder1.setShouldVibrate(true)
builder1.setGroupID("group56")
builder1.setVibrationPattern(longArrayOf(1000, 1000, 1000, 1000, 1000))
builder1.build()
val nChannelSets: MutableSet<NVNotificationChannels.Builder> = java.util.HashSet()
nChannelSets.add(builder1)
NotifyVisitorsApi.getInstance(activityContext).createNotificationChannel(nChannelSets)
GroupID
Once you are done with creating a group, you can pass groupID to connect your channels with it by adding the undermentioned code while creating channels.
builder1.setGroupID("group56");
builder1.setGroupID("group56")
Deleting Notification Channels
You can delete the notification channels created previously in your app. There is no error thrown when you try to delete a notification channel that doesn’t exist. You can delete the notification channels using the following line of code.
NotifyVisitorsApi.getInstance(activityContext).deleteNotificationChannel(String channelID);
NotifyVisitorsApi.getInstance(activityContext).deleteNotificationChannel(channelID: String?)
Kindly refer to the example provided below.
NotifyVisitorsApi.getInstance(activityContext).deleteNotificationChannelGroup("group56");
NotifyVisitorsApi.getInstance(activityContext).deleteNotificationChannelGroup("group56")
Deleting Notification Groups
NotifyVisitors SDK also allows you to remove the notification groups you have created previously. Please note that you will need to delete all the channels associated with a group prior to deleting a group. You can delete a notification group using the following line of code.
NotifyVisitorsApi.getInstance(activityContext).deleteNotificationChannelGroup(String groupId);
Example :
NotifyVisitorsApi.getInstance(activityContext).deleteNotificationChannelGroup("group56");
NotifyVisitorsApi.getInstance(activityContext).deleteNotificationChannelGroup(String groupId)
Example :
NotifyVisitorsApi.getInstance(activityContext).deleteNotificationChannelGroup("group56")
Notification Channel ID Settings in Dashboard
You can choose to have a notification channel ID as optional or mandatory for creating a push notification. Select the 'Notification Channels' option. It is available at Mobile Push → Configuration → Assets → Notification Channels. You can create a selection list of channel IDs for your messages.
By default, we have a channel called “General Notifications”. If you want to change the default setting then you can mark any of your custom channels as default in the dashboard.
After you are done with the setting whenever you create or send the push notification you can choose the channel among the defined channels in “Advanced Options” of the push notification.
Importance Level(s)
Channel Importance affects the interruption level of all notifications posted in the channel. There are five importance levels available, ranging from IMPORTANCE_NONE(0) to IMPORTANCE_HIGH(4). The importance level you assign to a channel applies to all notification messages that you post to it.
User-Visible Importance Level | Importance (Android 8.0 & higher) | Priority (Android 7.1 & lower) |
---|---|---|
Urgent Makes a sound and appears as a heads-up notification | IMPORTANCE_HIGH | PRIORITY_HIGH or PRIORITY_MAX |
High Makes a sound | IMPORTANCE_DEFAULT | PRIORITY_DEFAULT |
Medium No sound | IMPORTANCE_LOW | PRIORITY_LOW |
Low No sound and does not appear in the status bar | IMPORTANCE_MIN | PRIORITY_MIN |
Once you submit the channel to the NotificationManager, you cannot change the importance level. However, the user can change their preferences for your app's channels at any time.
IMPORTANCE | USAGE | EXAMPLES |
---|---|---|
HIGH | Time-critical information that the user must know, or act on, immediately | Text messages, alarms, phone calls |
DEFAULT | Information that should be seen at the user’s earliest convenience, but not interrupt what they're doing | Traffic alerts, task reminders |
LOW | Notification channels that don't meet the requirements of other importance levels | New content the user has subscribed to, social network invitations |
MIN | Non-essential information that can wait or isn’t specifically relevant to the user | Nearby places of interest, weather, promotional content. |
Updated 7 months ago