Android notification examples

A very simple default notification.

val NOTIFICATION_CHANNEL_ID = "lksjgioajw20u43knjak"
val mBuilder = NotificationCompat.Builder(this, "default")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("A simple notification")
        .setContentText("Swipe to dismiss it")
        .setChannelId(NOTIFICATION_CHANNEL_ID)
        .setAutoCancel(true)
val notification = mBuilder.build()

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@TargetApi(26)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID,
            "A simple notification",
            NotificationManager.IMPORTANCE_DEFAULT)
    notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(1, notification)

A very simple default notification, bring you back the main activity when click on it.

val NOTIFICATION_CHANNEL_ID = "lksjgioajw20u43knjak"
val backToAppIntent = Intent(this, MainActivity::class.java)
val pendingIntentBackToApp = PendingIntent.getActivity(this, 2222, backToAppIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val mBuilder = NotificationCompat.Builder(this, "default")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("Intent Notification")
        .setContentText("Multi lines content text. Back to app. Back to app. Back to app. Back to app. Back to app. Back to app. Back to app. Back to app.")
        .setStyle(NotificationCompat.BigTextStyle().bigText("Multi lines content text. Back to app. Back to app. Back to app. Back to app. Back to app. Back to app. Back to app. Back to app."))
        .setContentIntent(pendingIntentBackToApp)
        .setChannelId(NOTIFICATION_CHANNEL_ID)

val notification = mBuilder.build()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@TargetApi(26)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID,
            "A simple notification",
            NotificationManager.IMPORTANCE_DEFAULT)
    notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(2, notification)

Notification with action buttons. In this example, it sends broadcast actions when the action button is clicked.

val action1Intent = Intent()
action1Intent.action = ACTION1
val pendingIntentAction1 = PendingIntent.getBroadcast(this, 12345, action1Intent, PendingIntent.FLAG_UPDATE_CURRENT)
val action1 = NotificationCompat.Action.Builder(R.drawable.ic_adb_black_24dp, "Action 1", pendingIntentAction1).build()


val action2Intent = Intent()
action1Intent.action = ACTION2
val pendingIntentAction2 = PendingIntent.getBroadcast(this, 123456, action2Intent, PendingIntent.FLAG_UPDATE_CURRENT)
val action2 = NotificationCompat.Action.Builder(R.drawable.ic_android_black_24dp, "Action 2", pendingIntentAction2).build()


val mBuilder = NotificationCompat.Builder(this, "default")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("Action notification")
        .setContentText("Click the action buttons below.")
        .setChannelId(NOTIFICATION_CHANNEL_ID)
//                    .setOngoing(true)
        .addAction(action1)
        .addAction(action2)

val notification = mBuilder.build()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@TargetApi(26)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID,
            "A simple notification",
            NotificationManager.IMPORTANCE_DEFAULT)
    notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(3, notification)

Notification with custom view layout. In this example, there is a button which can be clicked and it will send a broadcast action.

val NOTIFICATION_CHANNEL_ID = "lksjgioajw20u43knjak"
val action3Intent = Intent()
action3Intent.action = ACTION3
val pendingIntentAction3 = PendingIntent.getBroadcast(this, 1234567, action3Intent, PendingIntent.FLAG_UPDATE_CURRENT)

val contentView = RemoteViews(packageName, R.layout.notification_layout)
contentView.setOnClickPendingIntent(R.id.btn_click, pendingIntentAction3)

val mBuilder = NotificationCompat.Builder(this, "default")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("collapsed title")
        .setContentText("collapsed text")
        .setCustomBigContentView(contentView)
        .setChannelId(NOTIFICATION_CHANNEL_ID)

val notification = mBuilder.build()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@TargetApi(26)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID,
            "A simple notification",
            NotificationManager.IMPORTANCE_DEFAULT)
    notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(4, notification)

Complete example in Github

Search within Codexpedia

Custom Search

Search the entire web

Custom Search