π Notifications π
First, add the permission in AndroidManifest.xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Then, since Android O, you need to create a channel. Developers should group their notifications by channels. For the user, they can do operations like disabling a notification channel.
- Create a Channel
- I pasted the code the gave in
XXX#onCreate
(XXX extends Application)
See also.
You also have to provide an icon, a title, a message, and a priority.
// priority is ignored on Android O (Channel importance is used instead)
val builder = NotificationCompat.Builder(applicationContext, channel_id)
.setSmallIcon(R.drawable.some_icon)
.setContentTitle("title")
.setContentText("body")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
with(NotificationManagerCompat.from(context)) {
// the "notificationId" is used to edit/close/...
// this notification later. Use any value >0.
notify(notificationId, builder.build())
}