Android Push Notification Channels were introduced in Android Oreo (Android 8.0 / API Level 26) to provide a system to help users manage the notifications. Users can now change preferences for an individual notification channel ( like opt-outs, vibration etc.) instead of changing it for all the notifications from a specific app.
What are notification channels?
When an app pushes notifications, you may want to only allow a certain type of notifications, for example, important notifications related to certain activities, to appear in the notification panel.
Prior to Android Oreo 8.0, you can enable or disable notifications for individual apps. You can also silence notification for individual apps.
But you cannot selectively disable or enable some notifications of an app.
In Android Oreo, developers can group notifications of their apps into different notification channels (aka notification categories). The user can disable or enable individual notification channel based on his/her preferences. For example, you can allow notifications from a specific notification channel to bypass do not disturb.
What do you need to do?
If your app targets Android Oreo and above, you must implement one or more notification channels to display notifications to your users. If you don’t target Android 8.0 but your app is used on devices running Android 8.0, your app behaves the same as it would on devices running Android 7.1 or lower.
The main feature of channels is that users will be presented with options to control them, rather than toggling all of the app’s notifications. That way, you’ll be able to reduce your opt-out rate by segmenting your notifications. Here’s how the system UI looks like:
How to create notification channel?
To create a notification channel, follow these steps:
- Construct a Notification Channel object with a unique channel ID, a user-visible name, and an importance level.
- Register the notification channel by passing it to createNotificationChannel().
1 2 3 4 5 6 7 8 |
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String channelId = "your_channel_id"; CharSequence channelName = "Your Channel"; int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance); notificationManager.createNotificationChannel(notificationChannel); |
How to push notification according to notification channels?
There is a function setChannel() which is used to create the notification according to the channels. After you receive the notification from firebase you can generate it according to channels and the user will be able to manage to see the notification according to his her needs.
1 2 3 4 5 6 7 8 9 10 11 12 |
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int notifyId = 1; String channelId = "your_channel_id"; Notification notification = new Notification.Builder(this, channelId) .setContentTitle("Your Message") .setContentText("This is some content!") .setSmallIcon(R.drawable.ic_app) .build(); notificationManager.notify(id, notification); |
That’s all you need to make your notifications perfectly work with Android Oreo.
Thank you very much. This is Vedesh Kumar signing off.