android kotlin - Notification inbox style example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the context
        val context = this

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)


        // Create and register notification channel api 26+
        val channelId = "My_Channel_ID"
        val notificationId = 1
        createNotificationChannel(channelId)


        // button click listener
        button.setOnClickListener{
            val notificationBuilder = NotificationCompat
                .Builder(context,channelId)
                .setSmallIcon(R.drawable.ic_action_help)
                .setContentTitle("Title: API LEVEL " + Build.VERSION.SDK_INT)
                .setContentText("Notification content text.")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setStyle(
                    NotificationCompat.InboxStyle()
                        // Append a line to the digest section
                        // of the Inbox notification.
                        .addLine("This is first line")
                        .addLine("This is second line")
                        .addLine("This is third line")
                        .addLine("This is fourth line")
                        .addLine("This is fifth line")

                        // Overrides ContentTitle in
                        // the big form of the template.
                        .setBigContentTitle("This is Content Title.")

                        // Set the first line of text
                        // after the detail section in
                        // the big form of the template.
                        .setSummaryText("This is summary text.")
                )


            with(NotificationManagerCompat.from(context)){
                notify(notificationId, notificationBuilder.build())
            }
        }
    }



    // create notification channel
    private fun createNotificationChannel(channelId:String) {
        // Create the NotificationChannel,
        // but only on API 26+ (Android 8.0) because
        // the NotificationChannel class is new and
        // not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val name = "My Channel"
            val channelDescription = "Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT

            val channel = NotificationChannel(channelId,name,importance)
            channel.apply {
                description = channelDescription
            }

            // Finally register the channel with system
            val notificationManager = getSystemService(
                Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Notification"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

android kotlin - Notification big text style example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the context
        val context = this

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)


        // Create and register notification channel api 26+
        val channelId = "My_Channel_ID"
        val notificationId = 1
        createNotificationChannel(channelId)


        // button click listener
        button.setOnClickListener{
            val notificationBuilder = NotificationCompat
                .Builder(context,channelId)
                .setSmallIcon(R.drawable.ic_action_help)
                .setContentTitle("Title: API LEVEL " + Build.VERSION.SDK_INT)
                .setContentText("Notification content text.")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setStyle(
                    NotificationCompat.BigTextStyle()
                        // Provide the longer text to be displayed in the big
                        // form of the template in place of the content text.
                        .bigText("Lorem ipsum dolor sit amet, consectetur" +
                                " adipiscing elit. Duis sed dapibus diam." +
                                " Maecenas nisi sapien, interdum eu enim vitae," +
                                " dapibus accumsan sem. Praesent porttitor quam" +
                                " purus, eget cursus erat viverra vitae. Nulla" +
                                " mollis, risus a blandit imperdiet, felis nunc" +
                                " dapibus nunc, vel blandit nisl risus non diam." +
                                " Vivamus eros risus, imperdiet viverra luctus" +
                                " sit amet, molestie vel nisl.")

                        // Overrides ContentTitle in the
                        // big form of the template.
                        .setBigContentTitle("This is Content Title.")

                        // Set the first line of text after the
                        // detail section in the big form of the template.
                        .setSummaryText("This is summary text.")
                )


            with(NotificationManagerCompat.from(context)){
                notify(notificationId, notificationBuilder.build())
            }
        }
    }



    // create notification channel
    private fun createNotificationChannel(channelId:String) {
        // Create the NotificationChannel,
        // but only on API 26+ (Android 8.0) because
        // the NotificationChannel class is new and
        // not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val name = "My Channel"
            val channelDescription = "Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT

            val channel = NotificationChannel(channelId,name,importance)
            channel.apply {
                description = channelDescription
            }

            // Finally register the channel with system
            val notificationManager = getSystemService(
                Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Notification"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

android kotlin - Notification big picture style example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the context
        val context = this

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)


        // Create and register notification channel api 26+
        val channelId = "My_Channel_ID"
        val notificationId = 1
        createNotificationChannel(channelId)


        // button click listener
        button.setOnClickListener{
            val notificationBuilder = NotificationCompat
                .Builder(context,channelId)
                .setSmallIcon(R.drawable.ic_action_help)
                .setContentTitle("Title: API LEVEL " + Build.VERSION.SDK_INT)
                .setContentText("Notification content text.")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setStyle(
                    NotificationCompat.BigPictureStyle()
                        // Provide the bitmap to be used as the
                        // payload for the BigPicture notification.
                        .bigPicture(BitmapFactory.decodeResource(
                            resources,R.drawable.flowers))

                        // Override the large icon when the
                        // big notification is shown.
                        .bigLargeIcon(BitmapFactory.decodeResource(
                            resources,R.drawable.flowers))

                        // Overrides ContentTitle in the
                        // big form of the template.
                        .setBigContentTitle("Beautiful Flower")

                        // Set the first line of text after the detail
                        // section in the big form of the template.
                        .setSummaryText("This is big picture summary")
                )


            with(NotificationManagerCompat.from(context)){
                notify(notificationId, notificationBuilder.build())
            }
        }
    }



    // create notification channel
    private fun createNotificationChannel(channelId:String) {
        // Create the NotificationChannel,
        // but only on API 26+ (Android 8.0) because
        // the NotificationChannel class is new and
        // not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val name = "My Channel"
            val channelDescription = "Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT

            val channel = NotificationChannel(channelId,name,importance)
            channel.apply {
                description = channelDescription
            }

            // Finally register the channel with system
            val notificationManager = getSystemService(
                Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Notification"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

android kotlin - Notification dismiss on action click

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the context
        val context = this

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)


        // Create and register notification channel api 26+
        val channelId = "My_Channel_ID"
        val notificationId = 1

        createNotificationChannel(channelId)

        // Create an explicit intent for an activity in this app
        val intent = Intent(context,MainActivity::class.java)
        intent.apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or
                    Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent = PendingIntent.getActivity(
            context,
            0,
            intent,
            PendingIntent.FLAG_IMMUTABLE or
                    PendingIntent.FLAG_UPDATE_CURRENT
        )


        val deleteIntent = Intent(context,MyBroadcastReceiver::class.java)
        deleteIntent.apply {
            action = "Delete"
            putExtra("UserId","100")
            putExtra("notificationId",notificationId)
        }
        val deletePendingIntent = PendingIntent.getBroadcast(
            context,
            0,
            deleteIntent,
            PendingIntent.FLAG_IMMUTABLE or
                    PendingIntent.FLAG_UPDATE_CURRENT
        )


        // button click listener
        button.setOnClickListener{
            val notificationBuilder = NotificationCompat
                .Builder(context,channelId)
                .setSmallIcon(R.drawable.ic_action_help_outline)
                .setContentTitle("Title: API LEVEL " + Build.VERSION.SDK_INT)
                .setContentText("Delete user on button click.")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .addAction(R.drawable.ic_action_delete,
                    "Delete User",deletePendingIntent)

            with(NotificationManagerCompat.from(context)){
                notify(notificationId, notificationBuilder.build())
            }
        }
    }



    // create notification channel
    private fun createNotificationChannel(channelId:String) {
        // Create the NotificationChannel,
        // but only on API 26+ (Android 8.0) because
        // the NotificationChannel class is new and
        // not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val name = "My Channel"
            val channelDescription = "Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT

            val channel = NotificationChannel(channelId,name,importance)
            channel.apply {
                description = channelDescription
            }

            // Finally register the channel with system
            val notificationManager = getSystemService(
                Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Notification"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
MyBroadcastReceiver.kt

package com.cfsuman.kotlintutorials

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.core.app.NotificationManagerCompat


class MyBroadcastReceiver: BroadcastReceiver(){
    override fun onReceive(context: Context?, intent: Intent?) {
        intent?.apply {
            val userId = getStringExtra("UserId")
            val notificationId = getIntExtra("notificationId",0)

            Toast.makeText(context,
                "Deleted ID: $userId",Toast.LENGTH_SHORT).show()

            context?.apply {
                // Remove the notification programmatically
                // on button click
                NotificationManagerCompat.from(this)
                    .cancel(notificationId)
            }
        }
    }
}
AndroidManifest.xml [add]

<receiver android:name=".MyBroadcastReceiver"/>
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

android kotlin - Notification multiple action buttons

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the context
        val context = this

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)


        // Create and register notification channel api 26+
        val channelId = "My_Channel_ID"
        createNotificationChannel(channelId)

        // Create an explicit intent for an activity in this app
        val intent = Intent(context,MainActivity::class.java)
        intent.apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or
                    Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent = PendingIntent.getActivity(
            context,
            0,
            intent,
            PendingIntent.FLAG_IMMUTABLE or
                    PendingIntent.FLAG_UPDATE_CURRENT
        )


        val deleteIntent = Intent(context,MyBroadcastReceiver::class.java)
        deleteIntent.apply {
            action = "Delete"
            putExtra("UserID","100")
        }
        val deletePendingIntent = PendingIntent.getBroadcast(
            context,
            0,
            deleteIntent,
            PendingIntent.FLAG_IMMUTABLE or
            PendingIntent.FLAG_UPDATE_CURRENT
        )


        val updateIntent = Intent(context,MyBroadcastReceiver::class.java)
        updateIntent .apply {
            action = "Update"
            putExtra("UserID","100")
            putExtra("Name","Jones")
        }
        val updatePendingIntent = PendingIntent
            .getBroadcast(
                context,
                0,
                updateIntent ,
                PendingIntent.FLAG_IMMUTABLE or
                PendingIntent.FLAG_UPDATE_CURRENT
            )


        // button click listener
        button.setOnClickListener{
            val notificationBuilder = NotificationCompat
                .Builder(context,channelId)
                .setSmallIcon(R.drawable.ic_action_help_outline)
                .setContentTitle("Title: API LEVEL " + Build.VERSION.SDK_INT)
                .setContentText("Delete or update user.")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .addAction(R.drawable.ic_action_delete,
                    "Delete User",deletePendingIntent)
                .addAction(R.drawable.ic_action_edit,
                    "Update User",updatePendingIntent)

            with(NotificationManagerCompat.from(context)){
                notify(1, notificationBuilder.build())
            }
        }
    }



    // create notification channel
    private fun createNotificationChannel(channelId:String) {
        // Create the NotificationChannel,
        // but only on API 26+ (Android 8.0) because
        // the NotificationChannel class is new and
        // not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val name = "My Channel"
            val channelDescription = "Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT

            val channel = NotificationChannel(channelId,name,importance)
            channel.apply {
                description = channelDescription
            }

            // Finally register the channel with system
            val notificationManager = getSystemService(
                Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Notification"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
MyBroadcastReceiver.kt

package com.cfsuman.kotlintutorials

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast


class MyBroadcastReceiver: BroadcastReceiver(){
    override fun onReceive(context: Context?, intent: Intent?) {
        val action = intent?.action

        if (action.equals("Delete")){
            val userID = intent?.getStringExtra("UserID")
            Toast.makeText(context,
                "Deleted ID: $userID",Toast.LENGTH_SHORT).show()
        }

        if (action.equals("Update")){
            val userID = intent?.getStringExtra("UserID")
            val name = intent?.getStringExtra("Name")
            Toast.makeText(context,
                "Update : $name",Toast.LENGTH_SHORT).show()
        }
    }
}
AndroidManifest.xml [add]

<receiver android:name=".MyBroadcastReceiver"/>
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

android kotlin - Notification action button example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import java.util.*
import kotlin.random.Random


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the context
        val context = this

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)


        // Create and register notification channel api 26+
        val channelId = "My_Channel_ID"
        createNotificationChannel(channelId)


        // Create an explicit intent for an activity in this app
        val intent = Intent(this,MainActivity::class.java)

        intent.apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or
                    Intent.FLAG_ACTIVITY_CLEAR_TASK
        }

        val pendingIntent = PendingIntent.getActivity(
            context,0,intent,
            PendingIntent.FLAG_IMMUTABLE or
                    PendingIntent.FLAG_UPDATE_CURRENT
        )


        val buttonIntent = Intent(this,MyBroadcastReceiver::class.java)

        buttonIntent.apply {
            action = "Do Pending Task"
            putExtra(
                "RandomNumber",
                "${Random.nextInt(500)}"
            )
        }


        val buttonPendingIntent = PendingIntent.getBroadcast(
            context,0,buttonIntent,
            PendingIntent.FLAG_IMMUTABLE or
                    PendingIntent.FLAG_UPDATE_CURRENT
        )


        // button click listener
        button.setOnClickListener{
            val notificationBuilder = NotificationCompat
                .Builder(this,channelId)
                .setSmallIcon(R.drawable.ic_action_help)
                .setContentTitle("Title: API LEVEL " + Build.VERSION.SDK_INT)
                .setContentText("UUID: " + UUID.randomUUID())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .addAction(R.drawable.ic_action_backup,
                    "Do Task",buttonPendingIntent)

            with(NotificationManagerCompat.from(this)){
                notify(1, notificationBuilder.build())
            }
        }
    }



    // create notification channel
    private fun createNotificationChannel(channelId:String) {
        // Create the NotificationChannel, but only on API 26+ (Android 8.0) because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val name = "My Channel"
            val channelDescription = "Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT

            val channel = NotificationChannel(channelId,name,importance)
            channel.apply {
                description = channelDescription
            }

            // Finally register the channel with system
            val notificationManager = getSystemService(
                Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Notification"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
MyBroadcastReceiver.kt

package com.cfsuman.kotlintutorials

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast

class MyBroadcastReceiver: BroadcastReceiver(){
    override fun onReceive(context: Context?, intent: Intent?) {
        val extra = intent?.getStringExtra("RandomNumber")

        //Toast.makeText(context,intent?.action,Toast.LENGTH_SHORT).show()
        Toast.makeText(
            context,
            "Random Number : $extra",
            Toast.LENGTH_SHORT
        ).show()
    }
}
AndroidManifest.xml [add]

<receiver android:name=".MyBroadcastReceiver"/>
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

android kotlin - Notification tap action example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import java.util.*


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the context
        val context = this

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)


        // Create and register notification channel api 26+
        val channelId = "My_Channel_ID"
        createNotificationChannel(channelId)


        // Create an explicit intent for an activity in this app
        val intent = Intent(this,MainActivity::class.java)

        intent.apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or
                    Intent.FLAG_ACTIVITY_CLEAR_TASK
        }

        val pendingIntent = PendingIntent.getActivity(
            context,0,intent,
            PendingIntent.FLAG_IMMUTABLE or
                    PendingIntent.FLAG_UPDATE_CURRENT
        )


        // button click listener
        button.setOnClickListener{
            val notificationBuilder = NotificationCompat
                .Builder(this,channelId)
                .setSmallIcon(R.drawable.ic_action_help)
                .setContentTitle("Title: API LEVEL " + Build.VERSION.SDK_INT)
                .setContentText("UUID: " + UUID.randomUUID())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)

            with(NotificationManagerCompat.from(this)){
                notify(1, notificationBuilder.build())
            }
        }
    }



    // create notification channel
    private fun createNotificationChannel(channelId:String) {
        // Create the NotificationChannel, but only on API 26+ (Android 8.0) because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val name = "My Channel"
            val channelDescription = "Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT

            val channel = NotificationChannel(channelId,name,importance)
            channel.apply {
                description = channelDescription
            }

            // Finally register the channel with system
            val notificationManager = getSystemService(
                Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Notification"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

android kotlin - Notification channel example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import java.util.*


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)


        // Create the notification channel before showing a notification
        createNotificationChannel()

        // button click listener
        button.setOnClickListener{
            val builder = NotificationCompat
                .Builder(this, "MY_CHANNEL_ID")
                .setSmallIcon(R.drawable.ic_action_help)
                .setContentTitle("Title: API LEVEL " + Build.VERSION.SDK_INT)
                .setContentText("Description : " + UUID.randomUUID())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)

            with(NotificationManagerCompat.from(this)) {
                notify(1, builder.build())
            }
        }
    }



    // function to create notification channel
    private fun createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name ="My Channel"
            val descriptionText = "Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(
                "MY_CHANNEL_ID",
                name,
                importance
            ).apply {
                description = descriptionText
            }


            // Register the channel with the system
            val notificationManager: NotificationManager =
                getSystemService(NOTIFICATION_SERVICE) as NotificationManager

            notificationManager.createNotificationChannel(channel)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Notification"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />