Blog Engineering

Creating Push Notifications in Android O – Part 1

Darshan Pania Darshan Pania, Associate Director with 12+ years in mobile tech, SDKs, and Gaming. Leads CleverTap's Mobile SDK & LiveOps team, passionate about innovative tech solutions.
Creating Push Notifications in Android O – Part 1

The latest Android OS version, Android Oreo, released on August 21st right in time with the Great American Eclipse. The latest version was deliciously named following the ritual to name Android OS versions on something sweet.
Here are a bunch of prominent Android O features that will catch your eye –

  • 2x Faster – Android O boasts of upto two times faster speed when powering up the OS and apps
  • Background limits – Android O will limit the background tasks for the apps you use the least, thereby freeing up resources for the apps you use regularly
  • Autofill – With your permission, Android O will remember credentials to speed up logins into your favorite apps
  • Picture-in-picture – This feature allows you to see two apps in one screen, so you can do a Whatsapp video call and check your email at the same time. That is cool!
  • Instant Apps – You won’t need to install an app to use it, Instant Apps allow you to use a specific app right from the Chrome browser
  • Notification dots – A small dot will appear on the app’s icon when you receive notifications and with a long press on the app icon, you can have a small view of the notifications right there.

Apart from the above eye-catching features, Android Oreo includes many subtle, behind the scene features like improved battery performance, new emojis on the Google keyboard, adaptive icons, background location limits, notification categories/channels, notification snoozing and a smart WiFi assistant.
Unlike iOS, Android still does not support GIFs or videos in its Rich Push Notifications.
After reading this blog post, you will be able to build push notifications using the latest features of Android O, i.e Notification Channels and Notification Badges in your app.

Prerequisites

Since Notification channels and app icon badges are the features of the latest Android version, you as a developer will have to update your SDK and Build Tools to the latest versions. The latest SDK tools version is 26.0.2 and the latest Build Tools version is 26.0.1. You will have to make sure that in your build.gradle file, the compileSdkVersion is 26 and buildToolsVersion is 26.0.1

android {
   compileSdkVersion 26
   buildToolsVersion "26.0.1"
}

I am also assuming that your app is using one of Firebase or GCM service providers for push notifications. If not, you can setup your app on Firebase and go through their documentation to use it in your app.

Notification Channels

Starting with Android O, notification channels allow you to create a custom user-controllable channel for each type of notification you want to display. When you target your app to Android O, you will have to implement one or more notification channels to display notifications to your users. If you don’t target Android O and your app is used on a device running Android O, then it will behave the way as it would on Android Nougat (7.0/7.1.1) or lower.
To create a notification channel –

  1. Construct a NotificationChannel object with a String ID unique to your app
  2. Configure the NotificationChannel object to the settings of your choice like alert sounds, lights and importance.
  3. Submit the notification channel to the notification manager.

Creating a notification channel with the same name and settings again doesn’t cause any problem, so you can create the notification channels at the launch of your app.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
   String id = "id_product";
   // The user-visible name of the channel.
   CharSequence name = "Product";
   // The user-visible description of the channel.
   String description = "Notifications regarding our products";
   int importance = NotificationManager.IMPORTANCE_MAX;
   NotificationChannel mChannel = new NotificationChannel(id, name, importance);
   // Configure the notification channel.
   mChannel.setDescription(description);
   mChannel.enableLights(true);
   // Sets the notification light color for notifications posted to this
   // channel, if the device supports this feature.
   mChannel.setLightColor(Color.RED);
   notificationManager.createNotificationChannel(mChannel);
}

Notification Badges

The new Android O launched a cool feature called Notification Dots/Badges which show notifications that a user has not dismissed on a long press of the app’s icon. By default, each new notification in a particular channel will increment the number displayed in the long-press menu. You can change the number appearing by setting a number of your choice while building the notification.
Android O Push Notification

Creating a Push Notification on Android Oreo

To create a notification, you will use the NotificationCompat.Builder class. The constructor which was used before took only Context as a parameter, but in Android O, the constructor looks like this –

NotificationCompat.Builder(Context context, String channelId)

We’ll be using Postman to send ourselves a push notification payload which looks like this

{
    "data": {
    "nm": "Trying out the Oreo",
    "nt": "Android O"
  },
  "to" : "Your FCM Token"
}

The following code snippet will show you how to create a notification –

Intent intent1 = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 123, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(),"id_product")
       .setSmallIcon(R.drawable.flatpnicon) //your app icon
       .setBadgeIconType(R.drawable.flatpnicon) //your app icon
       .setChannelId(id)
       .setContentTitle(extras.get("nt").toString())
       .setAutoCancel(true).setContentIntent(pendingIntent)
       .setNumber(1)
       .setColor(255)
       .setContentText(extras.get("nm").toString())
       .setWhen(System.currentTimeMillis());
notificationManager.notify(1, notificationBuilder.build());

Android O gives you a few more functions to customize your notification –

  • setNumber() – allows you to set the number displayed in the long-press menu
  • setChannelId() – allows you set the Channel Id explicitly if you are using the old constructor
  • setColor() – allows a RGB value to put a color theme for your notification
  • setBadgeIconType() – allows you to set an icon to be displayed in the long-press menu

Your notification will look like on Android O:
Android Oreo Push Notification
That’s it! This is all you need, to try out the cool new notification features of Android O.
In part 2 of this blog, I will help you understand how you can use CleverTap to leverage these features without any coding hassle. Stay tuned!

The Intelligent Mobile Marketing Platform

See how today’s top brands use CleverTap to drive long-term growth and retention.

Schedule a Demo Now!

Last updated on December 11, 2024