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

- 7,425
- 2
- 36
- 44

- 5,661
- 5
- 25
- 49
3 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)

- 7,425
- 2
- 36
- 44

- 5,661
- 5
- 25
- 49
-
1What's the difference in just showing the notification with the normal NotificationManager? – Florian Walther May 05 '18 at 08:43
-
@Florian Walther It makes notifications compatible with older versions. The channel id is being ignored for example. It would be silly to check if < API 26 ourselves every time we want to create a notification to pass the channelid or not – DennisVA Nov 18 '18 at 17:55
-
1Now with `androidx.appcompat:appcompat:1.1.0-rc01` you can use `NotificationManagerCompat.createNotificationChannel()` for API 26+. – Onkar Nene Jul 29 '19 at 10:08
-
Very neat. Do you know, by chance, how to make this notification appear also on Android Auto? – Josh Sep 19 '19 at 10:03
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
- Create and register notification channel.
- Create a notification.
- 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 :)

- 11,032
- 5
- 50
- 70
-
Given that replacement by AndroidCompat by androidx is [future-pointing and recommended](https://developer.android.com/jetpack/androidx/migrate), Darish has the most modern answer; I might hope razvan-cristian-lung @praful-bhatnagar would still be available to update their answers to reflect/point@ this migration? – anthropic android Jul 13 '19 at 07:30
-
-
1Maybe I'm missing something, but that method takes a list of android.app.NotificationChannel, which only exists on API 26, so how to create the channels to pass it without checking for API 26...? – androidguy Oct 10 '19 at 19:23
-
@androidguy You still have to check for API 26 while creating the NotificationChannel. Once the channel object is ready, you can use the NotificationManagerCompat to register it with the system. – Darish Oct 11 '19 at 07:39
I usually use this class to manage notification channels:
class NotificationManager(private val context: Context) {
companion object {
private val CHANNEL_ID = "YOUR_CHANNEL_ID"
private val CHANNEL_NAME = "Your human readable notification channel name"
private val CHANNEL_DESCRIPTION = "description"
}
@RequiresApi(Build.VERSION_CODES.O)
fun getMainNotificationId(): String {
return CHANNEL_ID
}
@RequiresApi(Build.VERSION_CODES.O)
fun createMainNotificationChannel() {
val id = CHANNEL_ID
val name = CHANNEL_NAME
val description = CHANNEL_DESCRIPTION
val importance = android.app.NotificationManager.IMPORTANCE_LOW
val mChannel = NotificationChannel(id, name, importance)
mChannel.description = description
mChannel.enableLights(true)
mChannel.lightColor = Color.RED
mChannel.enableVibration(true)
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
mNotificationManager.createNotificationChannel(mChannel)
}
}
Then you can use util like this
fun createNotificationCompatBuilder(context: Context): NotificationCompat.Builder {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return NotificationCompat.Builder(context, NotificationManager(context).mainNotificationId)
} else {
return NotificationCompat.Builder(context)
}
}
This way you can use it in any place of your application with signature just like you have used before and you can easily change it in case of future changes.

- 2,109
- 14
- 23
-
Very neat. Do you know, by chance, how to make this notification appear also on Android Auto? – Josh Sep 19 '19 at 10:03
-
@Josh sorry, haven't a chance to test notifications on Android Auto so can't advice here – A. Shevchuk Sep 19 '19 at 11:42