Create and Display Notification on Android

Create and Display Notification on Android with Example

Showing notification is one of the most important features in an Android app. Notifications are short messages which appear on home screen of the device. In this post we will learn how to programatically create and display a Notification 

Displaying a Simple Notification

Before strating Notification functionality it is important to understand what are the verious components involved in notification.

Android Notification

  1. Small Icon
  2. App Name (Given by System)
  3. Timestamp
  4. Content Title
  5. Content Text
  6. Large Icon

We will be using NotificationCompat.Builder class to display the notification as this is what will give us access to latest features and yet be compatible on versions as old Android 4.0

Creating a Notification

Notification.Builder is the best way to create a Notification object. We can define all the components of Notification mentioned above by set on the builder object. We can note that we pass a string as parameter to the Notification.Builder constructor, this string is the Notification Channel ID which means that this notification will now be a part of that particular channel on post Oreo devices. On pre-Oreo devices this parameter is ignored

// This is the Notification Channel ID. More about this in the next section
public static final String NOTIFICATION_CHANNEL_ID = "channel_id";

//Notification Channel ID passed as a parameter here will be ignored for all the Android versions below 8.0
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

builder.setContentTitle("This is Title Message");
builder.setContentText("This is Message Description");
builder.setSmallIcon(R.drawable.icon);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

Notification notification = builder.build();

 

Triggrer Notification

// Unique identifier for notification
public static final int NOTIFICATION_ID = 101;

//This is what will will issue the notification i.e.notification will be visible
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, notification);

 

Set Priority

We can set the Notification priority to display on notification tray by setPriority() method

PRIORITY_MIN : Lowest Priority. These notifications might not be shown to user unless under special circumstances
PRIORITY_LOW : Lower Priority. The UI may choose to show these items smaller, or at a different position in the list, compared with your app's PRIORITY_DEFAULT items.
PRIORITY_DEFAULT : Default priority.
PRIORITY_HIGH : Higher Priority. The UI may choose to show these items larger, or at a different position in notification lists, compared with your app's PRIORITY_DEFAULT items.
PRIORITY_MAX : Highest Priority. User is very likely to be immediately notified of this

notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

 

How to Set Sound to Notification?

Notification build has method setSound() will handle sound effect

notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));

 

We can also set Custom Notification Sound to notification by

notification.sound =Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.FILE_NAME);

 

Lock Screen Visibility

We can set  user to view your notification on lock screen

  1. VISIBILITY_PUBLIC shows the notification's full content.
  2. VISIBILITY_SECRET doesn't show any part of this notification on the lock screen.
  3. VISIBILITY_PRIVATE shows basic information, such as the notification's icon and the content title, but hides the notification's full content

notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

 

How to add Action buttons to Notification?

We can add actions to notification by below code

val pendingIntent = PendingIntent.getActivity(
    this,
    0, notificationIntent, 0
)
    builder.addAction(R.drawable.mtrl_ic_cancel,"Ok",pendingIntent)
    builder.addAction(R.drawable.mtrl_ic_arrow_drop_up,"Close",pendingIntent)

 

How to show BigText in Notification?

setCustomBigContentView is use to show big text on notification

Custom Notification

val remoteViews = RemoteViews(packageName, R.layout.notification_custom_layout)
remoteViews.setImageViewResource(R.id.notifAddDriverIcon, R.drawable.img_chef)

builder.setCustomBigContentView(remoteViews)


 

BigPictureStyle

Notification with BigPictureStyle

Instead of text we can even add an image to the expanded view of the notification. When in collapsed mode user sees small and large icon, only when expanded this image is also visible

builder.setStyle(
    NotificationCompat.BigPictureStyle()
        .bigPicture(BitmapFactory.decodeResource(resources, R.drawable.restaurant)))

 

How to update Notification?

To update existing notification we need to call notify() method with previous notification id

Removing a Notification

We can cancel all notification by using NotificationManager

To cancel single Notifiaction by 

manager.cancel(NOTIFICATION_ID)

T0 cancel All Notifications by

manager.cancelAll();