Lesson 190. Notifications. channels

Lesson 190. Notifications. channels


Android Oreo (API 26) has the ability to create message channels. In this lesson we will understand how to do it and why it is necessary.

For each application, the user can customize the message. To do this, go to the system settings, select Apps there, find in the list and open the desired program and select the Notifications section.

By default they look like this:

The settings are a little bit, and they will touch all the messages from this app.

Channels allow you to expand these settings and apply them selectively. The application developer creates a channel and specifies its channel ID when creating messages. The user in the system settings of the program sees this channel and can configure it: importance, sound, vibration and more. As a result, all messages belonging to this channel will be displayed with these settings.

That is, by creating a channel, the developer allows the user to customize the behavior of a particular group of messages.

Let’s create a channel:

NotificationManager notificationManager =
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
   NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "My channel",
           NotificationManager.IMPORTANCE_HIGH);
   channel.setDescription("My channel description");
   channel.enableLights(true);
   channel.setLightColor(Color.RED);
   channel.enableVibration(false);
   notificationManager.createNotificationChannel(channel);
}

Channels are only valid for Android Oreo and above, so we use Android version verification. Next, I will not include this check in the examples so as not to clutter the code.

In the NotificationChannel constructor we specify the ID, name and importance. Here are some other details and settings. By the name of the methods, everything is clear.

We create a channel using the createNotificationChannel method.

Now, the Notifications settings for your application look like this:

Two channels have appeared: default and our own My channel. Default settings will be used for messages for which no channel was specified.

Open My channel settings:

Note that Vibrate is off. We explicitly stated this when creating a feed using the enableVibration (false) method.

You can now specify a channel ID when creating messages, and messages will be displayed according to the settings of that channel.

NotificationCompat.Builder builder =
       new NotificationCompat.Builder(this, CHANNEL_ID)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Title")
               .setContentText("Notification text");

The channel ID is specified in the message builder constructor. And now this constructor will NOT be marked as Deprecated if you are using the appCompat library version 26 and above.

At what point to create a channel? You can start the program. Even if the channel has already been created, nothing will happen. But judging by the fact that the user cannot delete the channels, I think we can use any flag thread that we set to true after the first creation of the channels, and in the future it will tell us that the channels have already been created.

Group

Consider the example of a mail client. Suppose that it works not only with mail but also with a calendar. That is, it can send us messages of two types: letters and events.

Accordingly, we can create two channels, one for emails and the other for events. As a result, the user will be able to set up a separate message about the emails and separately about the events. It is convenient.

But our app does support multiple accounts. And for each account, we need to create two message channels.

When creating 4 channels, the settings will look like this:

You can improve this by using groups. Grouping is simply a way to visually split channels in settings.

Create a group like this:

NotificationManager notificationManager =
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannelGroup(
       new NotificationChannelGroup(GROUP_ID, "Group 1"));

In the constructor we specify an ID and a name.

Next, when creating a channel, we use the setGroup method to specify which group the channel will belong to.

NotificationChannel channel = new NotificationChannel(...);
// ...
channel.setGroup(GROUP_ID);
notificationManager.createNotificationChannel(channel);

So for each account of our application, we can create a group and specify it when creating message channels for that account:

User A (group)
Mail (channel)
Events (channel)
User B (group)
Mail (channel)
Events (channel)

The settings now look better:

Channels are grouped by account.

Getting channel information

At any time after you create a channel, you can get information about it.

NotificationChannel channel = notificationManager.getNotificationChannel(CHANNEL_ID);

The getNotificationChannel method will return a NotificationChannel or null object if a channel with the specified ID was not found. Using different channel get-methods, you will be able to find out how the user has set up your channel. But you won’t be able to reconfigure it, set methods just won’t work.

The only thing you can change is the name of the channel and its description. To do this, simply rebuild the channel with the new parameters and the same ID.

If you considered the channel settings and for some reason decided that the user was wrong, then you can ask him or her to change the settings.

For example, if a user has turned off message display for a channel, we open the settings for that channel.

NotificationChannel channel = notificationManager.getNotificationChannel(CHANNEL_ID);

if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
   Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
   intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
   intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
   startActivity(intent);
}

If getImportance is IMPORTANCE_NONE, it means that the channel has been disabled by the user. We create an Intent with the channel ID and package application and run Activity.

Of course, in a real application, you need to act not so clumsily, but first ask the user’s opinion and explain why you want him to change the channel settings.

deleting a channel

To delete a channel, use the deleteNotificationChannel method

notificationManager.deleteNotificationChannel(CHANNEL_ID);

Technically, of course, you can use delete and then create a feed to restore your settings. But it is not recommended to do so. In addition, in the settings at the bottom, the user will see how many channels have been deleted.

And he will understand that you just rebuild the channel and reset its settings.

Importance vs Priority

If you remember, when creating a message, we can give priority to the builder. Starting with Android Oreo, message priorities have been declared outdated and overridden by channel parameter – importance.




Discuss in the forum [1 reply]
Related Posts
Lesson 189. Notifications. message grouping

Android 7 (API 24) has the ability to group messages. Even if you don't explicitly implement it, the system Read more

Lesson 188. Notifications. custom messages

Android enables us to create a layout for messages ourselves. Consider a simple example: layout / notification.xml Height 64dp Read more

Lesson 187. Notifications. Action buttons. Reply.

Android 4.1 has the ability to add buttons to messages. This is done using the addAction method. Intent deleteIntent Read more

Lesson 186. Notifications. advanced messages

Android 4.1 (API 16) has advanced messages. Pulling down the message will display additional information. To create an advanced Read more

Leave a Comment