Table of contents |
|
Notifications in Android Oreo (8 ) |
|
Notification Channel Creation Method |
|
Notification Channel Parameters |
|
Notification Channel Creation Process |
|
Android Oreo has introduced significant changes, particularly in managing notifications within apps. Let's delve into the essential modifications required in the notification domain:
Prior to proceeding further, it is imperative to include the following line in the build.gradle (Module: app) dependencies:
Let's initiate by establishing a notification channel. The method presented below illustrates the creation of a notification channel:
// Import necessary classes
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import androidx.core.app.NotificationCompat;
public class Example {
private void issueNotification() {
// Create the notification channel for Android 8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
makeNotificationChannel( "CHANNEL_1", "Example channel", NotificationManager.IMPORTANCE_DEFAULT);
}
// Creating the notification builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CHANNEL_1");
// Setting the notification's properties
notificationBuilder
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notification!")
.setContentText("This is an Oreo notification!")
.setNumber(3);
// Getting the notification manager and sending the notification
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
// It is advisable not to use 0 as the notification ID, hence we used 1.
notificationManager.notify(1, notificationBuilder.build());
}
// Helper method to create a notification channel for Android 8.0
makeNotificationChannel(String channelId, String channelName, int importance)
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
Let's now delve into a detailed explanation of the functionality of this method.
First, the above method initially creates a notification channel with id = "CHANNEL_1" and the name "Example channel." The id isn’t visible anywhere but the name can be viewed by opening the "App notifications" option of the App Info page. Then a NotificationCompat.Builder object is made specifying the context and id as "CHANNEL_1." A different channel id can be mentioned provided it is made with the makeNotificationChannel() method. The rest is self-explanatory. The setNumber() method shows a number in the notification dot of the app. Finally, the notification id (here 1) is better to not set 0 as in cases where the notification is used for a Foreground service, it will fail to display if the id is 0. On executing the issueNotification() method, we get the following notification: |