Push Notification on Oreo

Push Notification on Oreo

Razia Sandhu

--

Hello guys, this story is on push notification on Oreo and upper versions of Android. Notifications are received on all notifications as usually we do, but there are some updates on Oreo and latest versions of Android. So let’s go to learn about push notifications. This story for those who already know how to implement notifications in Android application.

Oreo required a channel_id to show push notification. Firstly do setup of notifications as usually we do to receive notifications in Android application.

While adding code to generate notification create a channel for Nougat+

String CHANNEL_ID = "my_channel_01";// The id of the channel.
CharSequence name = getString(R.string.channel_name);// name of channel
int importance = NotificationManager.IMPORTANCE_HIGH;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
mChannel = new NotificationChannel(CHANNEL_ID, name, importance);

Here is the Notification builder code to generate notification:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)
.setColor(getResources().getColor(android.R.color.white))
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX)
.setChannelId(CHANNEL_ID)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message));

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.logo_v);
} else {
notificationBuilder.setSmallIcon(R.mipmap.logo);
}

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
notificationManager.createNotificationChannel(mChannel);

notificationManager.notify(new Random(System.currentTimeMillis()).nextInt(), notificationBuilder.build());

You can see that I have set different small icons. Small icon need to be vector icon on Lollipop and upper Android versions. Otherwise it will show colored dot only as small icon.

If you have PSD format icon then you can convert into SVG and then to vector file here are some online tools which you can use for conversion:

I hope you will find this story useful.

If you have stuck in any kind of firebase notification task and need help then don’t hesitate to ask me. :)

Enjoy coding and if you like this article don’t forget to like and share it.

--

--