Skip to content

Notification

Shyam Sundar Choudhary edited this page Jan 31, 2018 · 3 revisions

A notification is a message you display to the user outside of your app's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

Notification Manager and creating a notification

To create notifications you use the NotificationManager class which can be received from the Context, e.g. an activity or a service, via the getSystemService() method.

var notificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE)

The Notification.Builder provides a builder interface to create a Notification object. You use a PendingIntent to specify the action which should be performed once the user selects the notification.

The Notification.Builder allows you to add up to three buttons with definable actions to the notification.

A Notification object must contain the following:

  • A small icon, set by setSmallIcon().
  • A title, set by setContentTitle().
  • Detail text, set by setContentText().
  • On Android 8.0 (API level 26) and higher, a valid notification channel ID, set by setChannelId() or provided in the NotificationCompat.Builder constructor when creating a channel.
// prepare intent which is triggered if the
// notification is selected
val intent = Intent(this, NotificationReceiver::class.java)
// use System.currentTimeMillis() to have a unique ID for the pending intent
val pIntent = PendingIntent.getActivity(this, System.currentTimeMillis().toInt(), intent, 0)
// build notification
// the addAction re-use the same intent to keep the example short
val notification = Notification.Builder(this)
    .setContentTitle("New mail from " + "test@gmail.com")
    .setContentText("Subject")
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pIntent)
    .setAutoCancel(true)
    .addAction(R.drawable.icon, "Call", pIntent)
    .addAction(R.drawable.icon, "More", pIntent)
    .addAction(R.drawable.icon, "And more", pIntent).build()
// NOTIFICATION_ID is used to update the existing notification
notificationManager.notify(NOTIFICATION_ID, notification)

Android 4.1 and up also supports expanded notifications. You can set big style:

notification.setStyle(new Notification.BigTextStyle().bigText(longText))

or big custom view:

var bigView = RemoteViews(
                context.packageName,
                R.layout.big_notification_view
        )
notification.setCustomBigContentView(bigView)

You can not create list inside a notification.

Notification inbox style

You can apply expanded layout to your notification as:

val mBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Event tracker")
.setContentText("Events received")
val inboxStyle = NotificationCompat.InboxStyle()
val events = arrayOfNulls<String>(6)
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:")
...
// Moves events into the expanded layout
for (i in events.indices)
{
  inboxStyle.addLine(events[i])
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle)
...
// Issue the notification here.

Priority and importance

To set a notification's priority, call NotificationCompat.Builder.setPriority() and pass in one of the NotificationCompat priority constants. There are five priority levels, ranging from PRIORITY_MIN (-2) to PRIORITY_MAX (2); if not set, the priority defaults to PRIORITY_DEFAULT (0).

Android 8.0 (API level 26) deprecates the ability to set the priority levels of individual notifications. Instead, you can set a recommended importance level when creating a notification channel. You can configure a channel with one of five importance levels that configure the amount a channel can interrupt a user, ranging from IMPORTANCE_NONE(0) to IMPORTANCE_HIGH(4). The default importance level is 3 which displays everywhere, makes noise, but doesn't visually intrude on the user.

Removing Notification

Notification is removed when:

  • User dismisses or clicks(and you called setAutoCancel() during creation) the notification
  • You call cancel() for a specific notification ID or cancelAll(), which removes all of the notifications you previously issued.
  • If you set a timeout when creating a notification using setTimeoutAfter()

Read here about Notification Channels and Badges in Android 8.0.

Clone this wiki locally