Is there a way to set channels on Android Oreo when using NotificationManagerCompat
and NotificationCompat
?
ChannelId is a unique String to identify each NotificationChannel and is used in Notification.
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.
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.
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)
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'
The snippets below are in Kotlin, but you can also use Java if you want.
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)
}
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)
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With