Learn how you can Unlock Limitless Customer Lifetime Value with CleverTap’s All-in-One Customer Engagement Platform.
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 –
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.
Since Notification channels and 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.
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 –
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);
}
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.
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 –
Your notification will look like on Android O:
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