In this tutorial you will be learning how to create notifications in Android. You will learn how to make a simple notification. After that you will learn different styles of Notifications in Android. The complete code for this tutorial is available at this GitHub repo , you can clone/download the project and run in Android Studio.

Simple Notification

Here are the steps to create a very basic notification that has a title, text and an icon.

Get reference to NotificationManager . Build the notification with NotificationCompat . Builder . Send the notification!

private NotificationManager mNotificationManager; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1.Get reference to Notification Manager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //2.Build Notification with NotificationCompat.Builder Notification notification = new NotificationCompat.Builder(this) .setContentTitle("Simple Notification") //Set the title of Notification .setContentText("I am a boring notification.") //Set the text for notification .setSmallIcon(android.R.drawable.ic_menu_view). //Set the icon .build(); //Send the notification. mNotificationManager.notify(1, notification); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 private NotificationManager mNotificationManager ; @ Override protected void onCreate ( @ Nullable Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; //1.Get reference to Notification Manager mNotificationManager = ( NotificationManager ) getSystemService ( NOTIFICATION_SERVICE ) ; //2.Build Notification with NotificationCompat.Builder Notification notification = new NotificationCompat . Builder ( this ) . setContentTitle ( "Simple Notification" ) //Set the title of Notification . setContentText ( "I am a boring notification." ) //Set the text for notification . setSmallIcon ( android . R . drawable . ic_menu_view ) . //Set the icon . build ( ) ; //Send the notification. mNotificationManager . notify ( 1 , notification ) ; }

When building a Notification, you have to provide a title, text and icon. If you miss any one of them, then the app will crash when sending notification.

Android Oreo Notification Channels

In Android O (8.0) you are required to make a NotificationChannel when sending a notification, for every notification you will have to provide a NotificationChannel, if you don’t do so, then your notification will never show up to the user.

NotificationChannels are introduced in Android Oreo to give user precise control of which type of notification they want to see. For Example in a Shopping App you can multiple channel, one for ‘New Deals and Promotions’ and one for ‘Order Updates’, if the user doesn’t want notifications for new ‘New Deals and Promotions’, they can precisely turn off notifications from that channel. You can read more about NotificationChannels here.

In our app will will create a simple channel named ‘Default’. Here are the steps to create a NotificationChannel:

Define the channel name and id(must be unique). Check if you are on Android Oreo(8.0). Check if channel has already been created, if not then create the channel.

private static String DEFAULT_CHANNEL_ID = "default_channel"; private static String DEFAULT_CHANNEL_NAME = "Default"; /* * Create NotificationChannel as required from Android 8.0 (Oreo) * */ public static void createNotificationChannel(NotificationManager notificationManager) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //Create channel only if it is not already created if (notificationManager.getNotificationChannel(DEFAULT_CHANNEL_ID) == null) { notificationManager.createNotificationChannel(new NotificationChannel( DEFAULT_CHANNEL_ID, DEFAULT_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT )); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 private static String DEFAULT_CHANNEL_ID = "default_channel" ; private static String DEFAULT_CHANNEL_NAME = "Default" ; /* * Create NotificationChannel as required from Android 8.0 (Oreo) * */ public static void createNotificationChannel ( NotificationManager notificationManager ) { if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . O ) { //Create channel only if it is not already created if ( notificationManager . getNotificationChannel ( DEFAULT_CHANNEL_ID ) == null ) { notificationManager . createNotificationChannel ( new NotificationChannel ( DEFAULT_CHANNEL_ID , DEFAULT_CHANNEL_NAME , NotificationManager . IMPORTANCE _ DEFAULT ) ) ; } } }

Call this function once before sending any type of Notification and pass the channel id in the NotificationCompat.Builder.

private NotificationManager mNotificationManager; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1.Get reference to Notification Manager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); createNotificationChannel(mNotificationManager); //2.Build Notification with NotificationCompat.Builder Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Simple Notification") //Set the title of Notification .setContentText("I am a boring notification.") //Set the text for notification .setSmallIcon(android.R.drawable.ic_menu_view). //Set the icon .build(); //Send the notification. mNotificationManager.notify(1, notification); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 private NotificationManager mNotificationManager ; @ Override protected void onCreate ( @ Nullable Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; //1.Get reference to Notification Manager mNotificationManager = ( NotificationManager ) getSystemService ( NOTIFICATION_SERVICE ) ; createNotificationChannel ( mNotificationManager ) ; //2.Build Notification with NotificationCompat.Builder Notification notification = new NotificationCompat . Builder ( this , CHANNEL_ID ) . setContentTitle ( "Simple Notification" ) //Set the title of Notification . setContentText ( "I am a boring notification." ) //Set the text for notification . setSmallIcon ( android . R . drawable . ic_menu_view ) . //Set the icon . build ( ) ; //Send the notification. mNotificationManager . notify ( 1 , notification ) ; }

Notification With Intent

The previous notification we built didn’t opened anything when clicked. Now we will create a notification that takes and intent and fires the intent when clicked.

private NotificationManager mNotificationManager; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Create the intent to do a certain task (Here it is opening an activity) Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID) .setContentTitle(title) .setContentText(text) .setSmallIcon(android.R.drawable.ic_menu_send) .setAutoCancel(true) //Close notification after click .setContentIntent(pendingIntent); //Send the notification. mNotificationManager.notify(1, notification); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 private NotificationManager mNotificationManager ; @ Override protected void onCreate ( @ Nullable Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; mNotificationManager = ( NotificationManager ) getSystemService ( NOTIFICATION_SERVICE ) ; //Create the intent to do a certain task (Here it is opening an activity) Intent intent = new Intent ( context , MainActivity . class ) ; PendingIntent pendingIntent = PendingIntent . getActivity ( context , 0 , intent , PendingIntent . FLAG_UPDATE_CURRENT ) ; NotificationCompat . Builder builder = new NotificationCompat . Builder ( context , DEFAULT_CHANNEL_ID ) . setContentTitle ( title ) . setContentText ( text ) . setSmallIcon ( android . R . drawable . ic_menu_send ) . setAutoCancel ( true ) //Close notification after click . setContentIntent ( pendingIntent ) ; //Send the notification. mNotificationManager . notify ( 1 , notification ) ; }

First we create an intent that we want to be fired when user taps on the notification, them we create a pending intent from it to pass to the notification builder function setContentIntnet, read more about PendingIntent. We also use setAutoCancel(true), this dismisses the notification from notification tray after the user taps on it.

Styled Notification

Simple notifications only go so far, they look decent, have a good amount of functionality, but sometimes you just need a little bit of extra magic to make your notifications not only look better but also function better. This is were different notification styles come in. There are many in built notification styles within Android Framework, we will explore a handful of them.

Big Text Style Notification

As the name suggests this notification displays a larger text and should be used when you need to show a longer text to the user.

private NotificationManager mNotificationManager; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Create and configure the style NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle(); style.bigText(bigText); style.setBigContentTitle(title); style.setSummaryText(text); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID) .setContentTitle(title) .setContentText(text) .setSmallIcon(android.R.drawable.ic_menu_send) .setStyle(style); //Send the notification. mNotificationManager.notify(1, notification); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 private NotificationManager mNotificationManager ; @ Override protected void onCreate ( @ Nullable Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; mNotificationManager = ( NotificationManager ) getSystemService ( NOTIFICATION_SERVICE ) ; //Create and configure the style NotificationCompat . BigTextStyle style = new NotificationCompat . BigTextStyle ( ) ; style . bigText ( bigText ) ; style . setBigContentTitle ( title ) ; style . setSummaryText ( text ) ; NotificationCompat . Builder builder = new NotificationCompat . Builder ( context , DEFAULT_CHANNEL_ID ) . setContentTitle ( title ) . setContentText ( text ) . setSmallIcon ( android . R . drawable . ic_menu_send ) . setStyle ( style ) ; //Send the notification. mNotificationManager . notify ( 1 , notification ) ; }

We first create an instance of BigTextStyle and then set different texts:

setBigTextContentTitle : The title that shown with the big text.

: The title that shown with the big text. bigText : The big texts that need to be show.

: The big texts that need to be show. setSummaryText: The text that should be shown when notification is collapsed in the tray due to presence of other notifications.

Inbox Style Notification

Inbox Style notifications suits email type applications, you can see these types of notifications in gmail when your emails get accumulated.

(For convenience I have wrapped the code to create a notification in a function that returns the Notification, steps involved in showing the notification are the same).

public static Notification createInboxStyleNotification(Context context, String title, String text, String... lines) { NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); style.setSummaryText(text); style.setBigContentTitle("Big Content title - " + title); for (String line : lines) { style.addLine(line); } NotificationCompat.Builder builder = new NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID) .setContentTitle(title) .setContentText(text) .setSmallIcon(android.R.drawable.ic_menu_send) .setStyle(style); return builder.build(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static Notification createInboxStyleNotification ( Context context , String title , String text , String . . . lines ) { NotificationCompat . InboxStyle style = new NotificationCompat . InboxStyle ( ) ; style . setSummaryText ( text ) ; style . setBigContentTitle ( "Big Content title - " + title ) ; for ( String line : lines ) { style . addLine ( line ) ; } NotificationCompat . Builder builder = new NotificationCompat . Builder ( context , DEFAULT_CHANNEL_ID ) . setContentTitle ( title ) . setContentText ( text ) . setSmallIcon ( android . R . drawable . ic_menu_send ) . setStyle ( style ) ; return builder . build ( ) ; }

First we create an instance of InboxStyle, then to add a new line call the addLine function passing a String.

setSummaryText : Text to be shown instead of lines when notification collapsed.

: Text to be shown instead of lines when notification collapsed. setBigContentTitle: Title to be shown when notification is expanded i.e. when all the lines are visible.

Messaging Style Notification

Messaging Style notifications are well suited for messaging applications such as Whats app, Viber. You can use this notification when user’s messages get accumulated.

public static Notification createMessagingStyleNotification(Context context, String title, String text) { NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle("Gurleen Sethi"); style.addMessage(new NotificationCompat.MessagingStyle.Message("Testing", new Date().getTime(), "Saru Sethi")); style.addMessage(new NotificationCompat.MessagingStyle.Message("Testing 123", new Date().getTime(), "Saru Sethi")); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID) .setContentTitle(title) .setContentText(text) .setSmallIcon(android.R.drawable.ic_menu_send) .setStyle(style); //Set the notification style return builder.build(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 public static Notification createMessagingStyleNotification ( Context context , String title , String text ) { NotificationCompat . MessagingStyle style = new NotificationCompat . MessagingStyle ( "Gurleen Sethi" ) ; style . addMessage ( new NotificationCompat . MessagingStyle . Message ( "Testing" , new Date ( ) . getTime ( ) , "Saru Sethi" ) ) ; style . addMessage ( new NotificationCompat . MessagingStyle . Message ( "Testing 123" , new Date ( ) . getTime ( ) , "Saru Sethi" ) ) ; NotificationCompat . Builder builder = new NotificationCompat . Builder ( context , DEFAULT_CHANNEL_ID ) . setContentTitle ( title ) . setContentText ( text ) . setSmallIcon ( android . R . drawable . ic_menu_send ) . setStyle ( style ) ; //Set the notification style return builder . build ( ) ; }

First we create an instance of MessagingStyle, the we use the addMessage function to add messages, this functions takes in an object of MessagingStyle.Message. MessagingStyle.Message takes in constructor parameters:

text : The message to received from the sender.

: The message to received from the sender. timeStamp : Time at which message was received..

: Time at which message was received.. sender: The name of sender.

Big Picture Style Notification

When you want to show a picture in the notification you need to use Big Picture Style.

public static Notification createBigPictureStyleNotification(Context context, String title, String text, int bigPictureResource, int bigLargeIcon) { NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle(); style.setBigContentTitle(title); style.setSummaryText(text); style.bigPicture(BitmapFactory.decodeResource(context.getResources(), bigPictureResource)); style.bigLargeIcon(BitmapFactory.decodeResource(context.getResources(), bigLargeIcon)); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID) .setContentTitle(title) .setContentText(text) .setSmallIcon(android.R.drawable.ic_menu_send) .setStyle(style); return builder.build(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static Notification createBigPictureStyleNotification ( Context context , String title , String text , int bigPictureResource , int bigLargeIcon ) { NotificationCompat . BigPictureStyle style = new NotificationCompat . BigPictureStyle ( ) ; style . setBigContentTitle ( title ) ; style . setSummaryText ( text ) ; style . bigPicture ( BitmapFactory . decodeResource ( context . getResources ( ) , bigPictureResource ) ) ; style . bigLargeIcon ( BitmapFactory . decodeResource ( context . getResources ( ) , bigLargeIcon ) ) ; NotificationCompat . Builder builder = new NotificationCompat . Builder ( context , DEFAULT_CHANNEL_ID ) . setContentTitle ( title ) . setContentText ( text ) . setSmallIcon ( android . R . drawable . ic_menu_send ) . setStyle ( style ) ; return builder . build ( ) ; }

style.bigPicture takes in Bitmap so you need to convert your image into Bitmap.

style.bigLargeIcon also takes a Bitmap and show an icon when the notification is expanded.

That is it for this tutorial. Here are some other tutorials that you might like.

Getting started with Retrofit with Android.

A complete guide to integrating AdMob in you Android App.

How to make Bottom Sheet in Android.

Kotlin Data Class Tutorial.