Skip to content

Notification (Channels and badges)

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

Starting in Android 8.0 (API level 26), notification channels allow you to create a user-customizable channel for each type of notification you want to display. Notification channels provide a unified system to help users manage notifications. Users can visit Settings, or long-press a notification to change these behaviors, or even block a notification channel at any time

Creating a notification channel

val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// The id of the channel.
val id = "my_channel_01"
// The user-visible name of the channel.
val name = getString(R.string.channel_name)
// The user-visible description of the channel.
val description = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(id, name, importance)
mChannel.setDescription(description)
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.enableLights(true)
mChannel.setLightColor(Color.RED)
mChannel.enableVibration(true)
mChannel.setVibrationPattern(longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400))
mNotificationManager.createNotificationChannel(mChannel)

You can create multiple channel at once using createNotificationChannels().

To retrieve a single notification channel, you can call getNotificationChannel(). To retrieve all notification channels belonging to your app, you can call getNotificationChannels().

After you have the NotificationChannel, you can use methods such as getVibrationPattern() and getSound() to find out what settings the user currently has. To find out if a user blocked a notification channel, you can call getImportance(). If the notification channel is blocked, getImportance() returns IMPORTANCE_NONE.

Creating a notification channel group

// The id of the group.
val group = "my_group_01"
// The user-visible name of the group.
val name = getString(R.string.group_name)
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.createNotificationChannelGroup(NotificationChannelGroup(group, name))

Redirect user to notification channel setting

val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
intent.putExtra(Settings.EXTRA_CHANNEL_ID, mChannel.getId())
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName())
startActivity(intent)

Delete a channel

val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Channel id
val id = "my_channel_01"
mNotificationManager.deleteNotificationChannel(id)

Creating a notification

// The id of the channel.
val CHANNEL_ID = "my_channel_01"
val mBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
// Creates an explicit intent for an Activity in your app
val resultIntent = Intent(this, ResultActivity::class.java)
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your app to the Home screen.
val stackBuilder = TaskStackBuilder.create(this)
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity::class.java)
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent)
val resultPendingIntent = stackBuilder.getPendingIntent(
  0,
  PendingIntent.FLAG_UPDATE_CURRENT
)
mBuilder.setContentIntent(resultPendingIntent)
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// mNotificationId is a unique integer your app uses to identify the
// notification. For example, to cancel the notification, you can pass its ID
// number to NotificationManager.cancel().
mNotificationManager.notify(mNotificationId, mBuilder.build())

Notification badges

Android 8.0 (API level 26) introduces functionality for displaying notification badges on app icons in supported launchers. Notification badges show notifications associated with one or more notification channels in an app, which the user has not yet dismissed or acted on. Users can turn off badges for notification channels or apps from the Settings app.

Clone this wiki locally