Notifications — Android Development
A notification is a message that Android displays outside your app’s UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.
1. Create a Simple Notification
After Android Oreo, the concept of Channels
was introduced. Channels are different manners by which your notification can be opened. For example, in WhatsApp, a channel can be for group chat and for individual chat.
AndroidManifest.xml
Add this code snippet to provide permissions to POST a NOTIFICATION in your Android Application:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
MainAcitivity.kt
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
val nm : NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if(Build.VERSION.SDK_INT > Build.VERSION_CODE.O){
nm.createNotificationChannel(NotificationChannel("first", "default", NotificationManager.IMPORTANCE_DEFAULT))
}
button.setOnClickListener{
val simpleNotification = NotificationCompat.Builder(this, "first")
.setContentTitle("TITLE")
.setContentText("CONTENT")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
nm.notify(1,simpleNotification)
}
}
2. Clickable Notifications
Pending Intent creates the intent of the future. It is used if you want to use the notification after sometime from its arrival.
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
val nm : NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if(Build.VERSION.SDK_INT > Build.VERSION_CODE.O){
nm.createNotificationChannel(NotificationChannel("first", "default", NotificationManager.IMPORTANCE_DEFAULT))
}
button.setOnClickListener{
val i = Intent()
i.action = Intent.ACTION_VIEW
i.data = Uri.parse("<https://www.example.com>")
//val pi = PendingIntent.getActivity(getApplicationContext(), requestCode, intent, Flag)
val pi = PendingIntent.getActivity(getApplcaitionContext(), 123, i, Pending.FLAG_MUTABLE)
val clickableNotification = NotificationCompat.Builder(this, "first")
.setContentTitle("CLICKABLE_TEXT")
.setContentIntent(pi)
.setAutoCancel(true)
.setContentText("CONTENT")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
nm.notify(2,clickableNotification)
}
}
There was a problem in the code, it took me an hour to find it, the error was previously we were using the flag FLAG_UPDATE_CURRENT
after that I changed it to FLAG_MUTABLE
. And now it works fine.
To further add a Button in the notification we can use the following code by adding .addAction(``R.drawable.*ic_launcher_foreground*, "Click Me", pi)
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
val nm : NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if(Build.VERSION.SDK_INT > Build.VERSION_CODE.O){
nm.createNotificationChannel(NotificationChannel("first", "default", NotificationManager.IMPORTANCE_DEFAULT))
}
button.setOnClickListener{
val i = Intent()
i.action = Intent.ACTION_VIEW
i.data = Uri.parse("<https://www.example.com>")
//val pi = PendingIntent.getActivity(getApplicationContext(), requestCode, intent, Flag)
val pi = PendingIntent.getActivity(getApplcaitionContext(), 123, i, Pending.FLAG_MUTABLE)
val clickableNotification = NotificationCompat.Builder(this, "first")
.addAction(R.drawable.ic_foreground, "ClickMe", pi)
.setContentTitle("CLICKABLE_TEXT")
.setContentIntent(pi)
.setAutoCancel(true)
.setContentText("CONTENT")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
nm.notify(3,clickableNotification)
}
}
3. Heads — Up Notification
Final Code for MainActivity.kt
package com.example.notificationapp
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
@SuppressLint("UnspecifiedImmutableFlag")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val nm: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O){
val channel = NotificationChannel("first", "default", NotificationManager.IMPORTANCE_HIGH)
channel.apply{
enableLights(true)
enableVibration(true)
}
nm.createNotificationChannel(channel)
}
button.setOnClickListener{
val builder =
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
Notification.Builder(this, "first")
}
else{
Notification.Builder(this)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_VIBRATE or Notification.DEFAULT_LIGHTS)
}
val simpleNotification = builder
.setContentTitle("Simple Title")
.setContentText("This is sample description of the notification")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.build()
nm.notify(1,simpleNotification)
}
button2.setOnClickListener{
val i = Intent()
i.action = Intent.ACTION_VIEW
i.data = Uri.parse("<https://www.google.com>")
val pi = PendingIntent.getActivity(getApplicationContext(),123, i , PendingIntent.FLAG_MUTABLE)
val clickableNotification = NotificationCompat.Builder(this, "first")
.setContentTitle("Clickable Title")
.setContentIntent(pi)
.setAutoCancel(true) // Auto removes the notification after clicking
.setContentText("This is sample description of the notification")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
nm.notify(2,clickableNotification)
}
button3.setOnClickListener{
val i = Intent()
i.action = Intent.ACTION_VIEW
i.data = Uri.parse("<https://www.google.com>")
val pi = PendingIntent.getActivity(getApplicationContext(),123, i , PendingIntent.FLAG_MUTABLE)
val clickableNotification = NotificationCompat.Builder(this, "first")
.setContentTitle("Clickable with Button Title")
.addAction(R.drawable.ic_launcher_foreground, "Click Me", pi)
.setContentIntent(pi)
.setAutoCancel(true) // Auto removes the notification after clicking
.setContentText("This is sample description of the notification")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
nm.notify(3,clickableNotification)
}
}
}
Github Link to the Project
Link to the Application
https://drive.google.com/file/d/1adH75vHXEkQrSZUT38EW0eNPbaylg_az/view?usp=sharing
Resources
To create custom Notification Layouts, refer to the below link :