Notification Using Kotlin & JetPack

Arjun V
3 min readJul 26, 2023

--

Here I explain..

  1. Normal Notification
  2. Notification with button Action
  3. The notification includes EditText Reply
  1. Create a project including center aligned Button.
  2. Initialize channel ID here I used the same as my package name append with few characters and Notification Manager.
private val channelID = "com.example.notifications.channel1"
private var notificationManager: NotificationManager? = null

3. onCreate() method.

@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val button: Button = findViewById(R.id.button)
createNotification(channelID,"DEMO!","THIS IS.")
button.setOnClickListener {
displayNotification()
}
}

Inside onCreate 2 fun is there createNotification() for creating Notification and displayNotification() for display

4. Fun createNotification()

private fun createNotification(id:String,name:String,channelDescription:String){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(id,name,importance).apply {
description = channelDescription
}
notificationManager?.createNotificationChannel(channel)

}
}

5. Fun displayNotification()

@SuppressLint("NotificationPermission")
private fun displayNotification(){
val notificationId= 45
val notification : Notification = NotificationCompat.Builder(this,channelID)
.setContentTitle("Demo Title")
.setContentText("This is a Demo notification")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH).build()
notificationManager?.notify(notificationId,notification)
}

as a Result, we get a simple Notification.

←-For SetUp OnTap Navigation On Navigation →

make a few changes on displayNotification()

private fun displayNotification(){
///Intent
val tapResultIntent = Intent(this,SecondActivity::class.java)
val pendingIntent:PendingIntent = PendingIntent.getActivity(
this,
0,
tapResultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val notificationId= 45
val notification : Notification = NotificationCompat.Builder(this,channelID)
.setContentTitle("Demo Title")
.setContentText("This is a Demo notification")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH).build()
notificationManager?.notify(notificationId,notification)
}

←- Add Button On Navigation PopUP →

changes on displayNotification()

private fun displayNotification(){
val intent1 = Intent(this,DetailsActivity::class.java)
val pendingIntent1:PendingIntent =PendingIntent.getActivity(this,
0,
intent1,
PendingIntent.FLAG_UPDATE_CURRENT
)
val action1 : NotificationCompat.Action = NotificationCompat.Action.Builder(0,"Details",pendingIntent1).build()

val intent2 = Intent(this,SettingsActivity::class.java)
val pendingIntent2:PendingIntent =PendingIntent.getActivity(this,
0,
intent2,
PendingIntent.FLAG_UPDATE_CURRENT
)
val action2 : NotificationCompat.Action = NotificationCompat.Action.Builder(1,"Settings",pendingIntent2).build()
val notificationId= 45
val notification : Notification = NotificationCompat.Builder(this,channelID)
.setContentTitle("Demo Title")
.setContentText("This is a Demo notification")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setAutoCancel(true)
.addAction(action1)
.addAction(action2)
.setPriority(NotificationCompat.PRIORITY_HIGH).build()
notificationManager?.notify(notificationId,notification)
}

Add two buttons on Navigation(Details and Settings).

←- Add EditText On Navigation PopUP And Pass The Text to Another Page→

private val KEY_REPLY = "key_reply"

Initialize a Key for passing text data at first

@SuppressLint("NotificationPermission")
private fun displayNotification(){
//renote action for reply input
val remoteInputs : RemoteInput = RemoteInput.Builder(KEY_REPLY).run {
setLabel("Insert your name here...!")
build()
}
//remenber to add android x package of REmoteInput
val replyAction : NotificationCompat.Action = NotificationCompat.Action.Builder(
0,"REPLEY",pendingIntent2
).addRemoteInput(remoteInputs).build()

val intent2 = Intent(this,SettingsActivity::class.java)
val pendingIntent2:PendingIntent =PendingIntent.getActivity(this,
0,
intent2,
PendingIntent.FLAG_UPDATE_CURRENT
)

val notificationId= 45
val notification : Notification = NotificationCompat.Builder(this,channelID)
.setContentTitle("Demo Title")
.setContentText("This is a Demo notification")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setAutoCancel(true)
.addAction(replyAction)
.setPriority(NotificationCompat.PRIORITY_HIGH).build()
notificationManager?.notify(notificationId,notification)
}

Here we pass text Inside EditText to SettingsActivity.kt

SettingsActivity.kt

package com.example.notifications
import android.app.NotificationManager
import android.app.RemoteInput
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.core.app.NotificationCompat

class SettingsActivity : AppCompatActivity() {
private val KEY_REPLY = "key_reply"
lateinit var textView : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
textView = findViewById<TextView>(R.id.textView2)
receiveInput()
}

private fun receiveInput(){
val channelID = "com.example.notifications.channel1"
val notificationId= 45
val intent = this.intent
val remoteInput = RemoteInput.getResultsFromIntent(intent)
if(remoteInput!=null){
val inputString = remoteInput.getCharSequence(KEY_REPLY).toString()
textView.text = inputString.toString()
val replyNotification= NotificationCompat.Builder(this,channelID).setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentText("Your reply received").build()
val notificationManager : NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId,replyNotification)

}
}

}

GitHUb : https://github.com/Arjun-00/Notifications

--

--