As the Android user and developer, this is one of the most features that I like in Android Oreo release. Now let’s discuss, what’s the Notification channels?

Notification Channels is the feature that enables developers to group notifications according to different categories. Developers can set various configurations to each group and end users also have the ability to change the settings to each notification group.

Steps to create

  1. In your build.gradle file set compileSdkVersion to 26
  2. Change targetSdkVersion to 26
  3. Sync the gradle,  it’ll download latest Android Oreo SDK release to your development environment
  4. For creating number of notification channels you need to follow this syntax
    
private NotificationManager sManager;

public NotificationManager getManager() {
    if (sManager == null) {
        sManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }
    return mManager;
}

public void createChannels() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        try {

            // create negotiation channel
            NotificationChannel channel1 = new NotificationChannel(CHANNEL_ID_1, CHANNEL_NAME_1, NotificationManager.IMPORTANCE_HIGH);
            channel1.enableLights(true);
            channel1.enableVibration(true);
            channel1.setLightColor(Color.GREEN);
            channel1.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), audioAttributes);
            channel1.setShowBadge(true);
            channel1.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(channel1);

            // create current trip channel
            NotificationChannel channel2 = new NotificationChannel(CHANNEL_ID_2, CHANNEL_NAME_2, NotificationManager.IMPORTANCE_DEFAULT);
            channel2.enableLights(true);
            channel2.enableVibration(false);
            channel2.setLightColor(Color.BLUE);
            channel2.setShowBadge(true);
            channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(channel2);

            // create reminders channel
            NotificationChannel channel3 = new NotificationChannel(CHANNEL_ID_3, CHANNEL_NAME_3, NotificationManager.IMPORTANCE_LOW);
            channel3.enableLights(false);
            channel3.enableVibration(false);
            channel3.setShowBadge(false);
            channel3.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(channel3);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

You need to call above methods only once because it just used for creating the notification channels inside user’s device. Now we have Notification Channels ready, let’s see how can we send the notification according to different groups

Create the notification

Creating the notification is too much easy, you need to just pass channel id as an argument to NotificationCompat.Builder class. But for this,

  1. you need to add this string to your gradle compilation block
    compile 'com.android.support:support-v4:26.0.2'
  2. Add newly build google repository to maven repository block
    repositories {
        maven { url "https://maven.google.com/" }
    }
    
  3. Sync the project
  4. Create NotificationCompat.Builder object and supply title, description and icon. But remember, in the constructor, you need to pass notification channel id and that’s compulsory.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_operr_logo_white)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(message)
                    .setAutoCancel(true);
    
    getManager().notify(NOTIFICATION_ID, builder.build());
    

    NotificationCompat supports backward compatibility. So you don’t need to check for the device OS version and create multiple notifications according to that.

Try above steps in your code and if you fond any problem, please add a comment below


Leave a Reply

Related Posts

Android

Create apps with MVP architecture

Most of the guys heard about MVC(Model – View – Controller) architecture. So what’s new in this and how it helps me in Android? First of all, MVP(Model – View – Presenter) is derived from Read more…