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.

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

notifications

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:

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

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

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.

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.

notification

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

  • setBigTextContentTitle: The title that shown with the big text.
  • bigText: 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).

notification

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.
  • setBigContentTitle: Title to be shown when notification is expanded i.e. when all the lines are visible.

Never miss a post from TheTechnoCafe


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.

notification

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.
  • timeStamp: 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.

notification

  • 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.


Gurleen Sethi

How can I tell you about myself in just a few lines, to get to know me you can reach out to me, email, text, call, or any other method of communication you like. I am always there for you!

3 Comments

Shahbaz · July 12, 2018 at 4:56 am

This function is deprecated :
NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle("Gurleen Sethi");

Secondly, svg images are not rendered in BigPictureStyle Notification

    Gurleen Sethi · July 15, 2018 at 6:59 am

    It has been deprecated recently.
    The new one is NotificationCompat.MessagingStyle(Person).

خانه اندروید · July 20, 2018 at 4:34 am

tankyou admin
very good learn

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × 3 =