Notifications in Android Oreo (8+)

Android Oreo has brought in a ton of changes. This also includes the way in which a user issue notifications in an app. In this article, we will discuss the changes required to be made in the notification department.

Following things are to be kept in mind while issuing notifications:

  1. Designing a Notification Channel
  2. Importance of each notification channel
  3. Notification ID (different from channel id) should not be set to zero.

Before going forward, make sure this line is added in the build.gradle (Module: app) dependencies:



implementation 'com.android.support:appcompat-v7:26.1.0'

Let’s start with making a notification channel. The method below creates a notification channel:

filter_none

edit
close

play_arrow

link
brightness_4
code

@RequiresApi(api = Build.VERSION_CODES.O)
void makeNotificationChannel(String id, String name, int importance)
{
    NotificationChannel channel = new NotificationChannel(id, name, importance);
    channel.setShowBadge(true); // set false to disable badges, Oreo exclusive
  
    NotificationManager notificationManager =
         (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  
    assert notificationManager != null;
    notificationManager.createNotificationChannel(channel);
}

chevron_right


Let’s see what this method does in detail.

  1. The method accepts String id, String name, int importance..
    • String id: This is the id with which you issue a notification in the notification channel. You use this exact id for multiple notifications in the same channel.
    • String name: This is the name of the channel visible when someone taps navigates to Settings -> Apps & Notifications -> [your_app_name] -> App notifications.
    • int importance: This is the importance level of the channel. The levels are as follows:

      1. NotificationManager.IMPORTANCE_MIN – shows only in notification shade, no sound or peek.
      2. NotificationManager.IMPORTANCE_LOW – shows everywhere, doesn’t make sound, doesn’t peek.
      3. NotificationManager.IMPORTANCE_DEFAULT – shows everywhere, makes sound but doesn’t peek.
      4. NotificationManager.IMPORTANCE_HIGH – shows everywhere, makes sound and peeks (visual interruption).
      5. NotificationManager.IMPORTANCE_MAX – this importance level is usually not used. Works similar to IMPORTANCE_HIGH.
      6. Often the IMPORTANCE_DEFAULT parameter is preferred but this produces the notification sound that can be annoying. To silence, add the following line in the makeNotificationChannel() method, just before the last line.

        channel.setSound(null, null);
        
  2. The method then creates the channel with the parameters.
  3. The setShowBadge(true) makes the notification available by the Oreo notification dot feature.
  4. Finally, the notification channel is created by the createNotificationChannel() method of NotificationManager.
  5. Now let us issue a notification. We will use NotificationCompat for backwards compatibility.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    void issueNotification()
    {
      
        // make the channel. The method has been discussed before.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            makeNotificationChannel("CHANNEL_1", "Example channel", NotificationManager.IMPORTANCE_DEFAULT);
        }
        // the check ensures that the channel will only be made
        // if the device is running Android 8+
      
        NotificationCompat.Builder notification =
                    new NotificationCompat.Builder(this, "CHANNEL_1");
        // the second parameter is the channel id.
        // it should be the same as passed to the makeNotificationChannel() method
      
        notification
            .setSmallIcon(R.mipmap.ic_launcher) // can use any other icon
            .setContentTitle("Notification!")
            .setContentText("This is an Oreo notification!")
            .setNumber(3); // this shows a number in the notification dots
      
        NotificationManager notificationManager =
                    (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
      
        assert notificationManager != null;
        notificationManager.notify(1, notification.build());
        // it is better to not use 0 as notification id, so used 1.
    }

    chevron_right

    
    

    The execution proceeds in this way:

    • 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:



    My Personal Notes arrow_drop_up


    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.