android kotlin - Notification messaging style example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.*
import android.content.Context
import android.graphics.drawable.Icon
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{
            if (Build.VERSION.SDK_INT>=28) {
                val builder = Notification
                    .Builder(context,channelId)
                    .setContentTitle("This is title")
                    .setContentText("This is content description")
                    .setSmallIcon(R.drawable.ic_action_help)

                // Person added in API level 28
                val jenny = Person.Builder()
                    .setIcon(Icon.createWithResource(
                        context,R.drawable.ic_action_edit))
                    .setName("Jenny")
                    .build()
                val faria = Person.Builder()
                    .setIcon(Icon.createWithResource(
                        context,R.drawable.ic_action_backup))
                    .setName("Faria")
                    .build()

                // Notification.MessagingStyle added in API level 24
                builder.style = Notification.MessagingStyle(jenny)
                    .addMessage("Hi! how are you?",
                        System.currentTimeMillis()-60000,jenny)
                    .addMessage("I Am fine",
                        System.currentTimeMillis()-30000,faria)
                    .addMessage("Good.",
                        System.currentTimeMillis(),jenny)
                    .setGroupConversation(true)

                with(NotificationManagerCompat.from(context)){
                    notify(notificationId, builder.build())
                }
            }else{
                val builder = NotificationCompat
                    .Builder(context,channelId)
                    .setContentTitle("This is title")
                    .setContentText("This is content description")
                    .setSmallIcon(R.drawable.ic_action_help)

                builder.setStyle(NotificationCompat
                    .MessagingStyle("jenny")
                    .addMessage("Hi! how are you?",
                        System.currentTimeMillis()-60000,"Jenny")
                    .addMessage("I Am fine",
                        System.currentTimeMillis()-30000,"Faria")
                    .addMessage("Good.",
                        System.currentTimeMillis(),"Jenny")
                    .setGroupConversation(true)
                )

                with(NotificationManagerCompat.from(context)){
                    notify(notificationId, builder.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" />