Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotificationManagerCompat on Android Oreo

Is there a way to set channels on Android Oreo when using NotificationManagerCompat and NotificationCompat?

like image 489
Razvan Cristian Lung Avatar asked Aug 25 '17 09:08

Razvan Cristian Lung


People also ask

What is Channel ID in Android notification?

ChannelId is a unique String to identify each NotificationChannel and is used in Notification.

What is the use of notification channel in Android?

What Are Notification Channels on Android? “Notification Channels” were introduced in 2017 with Android 8.0 Oreo. The idea is that apps can group different types of notifications into “channels.” Each channel can then be turned on or off by the user. This all happens in the Android settings.

What is NotificationManagerCompat?

Using NotificationManagerCompat with AndroidX is the recommended way. NotificationManagerCompat now supports Notification channels. The new version Added Notification channels methods to NotificationManagerCompat so developers can use only NotificationManagerCompat when working with notifications.


2 Answers

Since NotificationManagerCompat is just a wrapper class that makes life easier, you can create the channels normally:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val name = getString(R.string.channel_title)
    val description = getString(R.string.channel_description)
    val importance = NotificationManager.IMPORTANCE_HIGH
    val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
    mChannel.description = description
    mChannel.enableLights(true)
    mChannel.lightColor = Color.parseColor("#5B3C88")
    mChannel.enableVibration(true)
    mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
    val manager = (context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
    manager.createNotificationChannel(mChannel)
}

And then use the NotificationManagerCompat when you post the notifications, but don't forget to construct the notification using the new constructor:

NotificationCompat.Builder(context, CHANNEL_ID)
like image 174
Razvan Cristian Lung Avatar answered Nov 28 '22 23:11

Razvan Cristian Lung


Using NotificationManagerCompat with AndroidX is the recommended way.

NotificationManagerCompat now supports Notification channels. The new version Added Notification channels methods to NotificationManagerCompat so developers can use only NotificationManagerCompat when working with notifications.

For Java, include the following in your build.gradle file

implementation 'androidx.core:core:1.2.0'

For Kotlin, include the following instead of the above dependency in your build.gradle file

implementation 'androidx.core:core-ktx:1.2.0'

To display a notificaiton, you will have to do the following

  1. Create and register notification channel.
  2. Create a notification.
  3. Show the notification

The snippets below are in Kotlin, but you can also use Java if you want.

1. Create and register a notification channel.

Notification channels provide a common visual and auditory experience for notifications of a similar type. Since their introduction in API 26, you are now required to set a channel for a notification, otherwise they will not display on newer versions of Android.

So define a helper method as shown below to create a notification channel for you.

//define your channel id
val CHANNEL_ID = "com.yourpackagename.your_channel_id"

//create notification channel for android Oreo and above devices.
if (Build.VERSION.SDK_INT >= 26) {
    val channel = NotificationChannel(CHANNEL_ID , "Your channel name", NotificationManager.IMPORTANCE_DEFAULT)
    NotificationManagerCompat.from(this).createNotificationChannel(channel)
}

2. Create a notification.

Use the NotificationCompat.Builder to create a Notificaiton. Please note that the CHANNEL_ID is passed to the builder.

var builder = NotificationCompat.Builder(this, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Much longer text that cannot fit one line...")
    .setStyle(NotificationCompat.BigTextStyle()
            .bigText("Much longer text that cannot fit one line..."))
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)

3. Show the notification

To make the notification appear, call NotificationManagerCompat.notify(), passing it a unique ID for the notification and the result of NotificationCompat.Builder.build()

   NotificationManagerCompat.from(this).notify(notificationId, builder.build())

That's all :)

like image 41
Darish Avatar answered Nov 29 '22 00:11

Darish