Sitemap

Custom Notifications in android

5 min readAug 3, 2021

--

Here, we are going to give notifications a custom look as your need in your android application.

Press enter or click to view image in full size

Table of content

Intro

Create a notification channel

Set content of the notification

Set styles of the notification

Create custom layout for notification

Show notification

Notifications alert you to many things on your Android device, including incoming messages, new emails, and calendar events. Notifications work slightly differently for each app, but they’re easy to customize.

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all.

You can’t change notification behavior (like channel ID ), after creating a notification channel, but still you can modify channel name and channel description.

You can customize the notifications as your need through setting up different styles , adding action buttons, showing images, playing different sounds while notification arrives, creating custom layouts for notification.

Create a notification channel

To create a notification channel, follow these steps:

  1. Construct a NotificationChannel object with a unique channel ID, a user-visible name, and an importance level.
  2. Optionally, specify the description that the user sees in the system settings with setDescription().
  3. Register the notification channel by passing it to createNotificationChannel().

Get Notification manager

Note: The CHANNEL_ID should be unique for every notification channel in order to handle this channel in future if we want to modify or delete this channel by using the unique channel_id.

By default, all notifications posted to this channel use the visual and auditory behaviors defined by the importance level from the NotificationManagerCompat class, such as IMPORTANCE_DEFAULT and IMPORTANCE_HIGH.

For more information about importance level you can visit here.

Set the content of the notification

To get started, you need to set the notification’s content and channel using a NotificationCompat.Builder object.

  • setContentTitle() - For title of the notification
  • setContentText() - For additional description of the notification
  • setSmallIcon() - For adding an icon when notification drawer is closed

These are the basic things to show in a notification.

Note: You must have to setSmallIcon() otherwise you will get an IllegalArgumentException when you try to build the NotificationCompat.Builder

This is the basic layout of the notification

Press enter or click to view image in full size
Simple Notification

Some more methods to customize your notification:

  • setAutoCancel(true) - To remove notification from notifications drawer after clicking it
  • setOngoing(true) - To make notification uncancelable
  • setLargeIcon() - To add an large icon to notification (visible only in notification drawer)

Notification with large image

Press enter or click to view image in full size
  • setSound(soundUri) - To set a custom sound to the notification

How to get soundUri ?

Follow this :

First add a mp3 file to the raw resource folder (say, sound.mp3)

Then , use the below code to get soundUri

Note: For API 26+, the sound should be added in NotificationChannel also

Use below code for this:

  • setContentIntent(pendingIntent) - To open desired activity in the application on clicking of the notification
  • setStyle() - To set your desired style to the notification (e.g., BigPictureStyle , BigTextStyle, MessagingStyle etc. )

Set styles of the notification

We are going to use setStyle() method of NotificationCompat.Builder to set different styles to the notification.

Add a large block of text

Apply NotificationCompat.BigTextStyle() to display a large block of text in notification,

Press enter or click to view image in full size

Upper one — Collapsed

Get Hrithik Sharma’s stories in your inbox

Join Medium for free to get updates from this writer.

Lower one — Expanded

Add a large expandable image

Apply NotificationCompat.BigPictureStyle() to show a large expandable image,

setLargeIcon() sets the image as a thumbnail when the notification is collapsed , that's why set bigLargeIcon(null) so that the largeIcon (thumbnail) goes away when the notification expands.

Press enter or click to view image in full size

Inbox Styled Notification

Apply NotificationCompat.InboxStyle() to make your notification inbox styled.

Press enter or click to view image in full size

To add more lines to the notification call addLine().

You should call addLine() more than 6 times because more than 6 times will show only first 6 lines.

Conversational Notification

Apply NotificationCompat.MessagingStyle() to your notification style and you will get notification with sequential messaging layout.

NotificationCompat.MessagingStyle.Message Parameters:

  • text (1st parameter) — The message of the sender
  • timestamp (2nd parameter) — Time of the message sent
  • sender (3rd parameter) — Sender of the message

Note: Messages in the notification will be sorted according to the timestamp of the message

Press enter or click to view image in full size

Upper one — Collapsed

Lower one — Expanded

Create custom layout for notification

  • First create a layout for the notification

Remember that , constraint layout is not allowed for custom notification’s layout

notification_layout.xml

  • Instantiating a RemoteViews object and set it to the notification,
Press enter or click to view image in full size

Show notification

To show the notification call mManager.notify() and pass a unique id of the notification and also the Notification which can be get by mBuilder.build()

Remember that notificationId should be unique int and will require when you want to update or remove the notification.

getManager() is defined here

You can see the source code here — CustomNotification

Feedback and suggestions are welcome.

I hope you find it useful or at least it gives you some fresh ideas.

Thanks for reading 🙌🏼

Happy coding😀😀

BTW, I solve real-world problems by building useful apps. Have a look at our portfolio.

Connect with me on Github and LinkedIn

--

--

Hrithik Sharma
Hrithik Sharma

I am an Android Developer, built products that people will use to solve real world problems.