Scheduling android notifications

What you will learn in this lesson?

Step 1: Create a Notification Channel

private fun createChannel(channelId: String, channelName: String) {
//create a channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(
channelId,
channelName,
// change importance of notication
NotificationManager.IMPORTANCE_HIGH
)//disable badges for this channel
.apply {
setShowBadge(false)
}

notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationChannel.description = getString(R.string.notification_channel_description)

val notificationManager = requireActivity().getSystemService(
NotificationManager::class.java
)
notificationManager.createNotificationChannel(notificationChannel)

}
}
Notification importance levels
createChannel(
getString(R.string.notification_channel_id),
getString(R.string.notification_channel_name)
)
class AlarmReceiver: BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
// sendNotification
val notificationManager = ContextCompat.getSystemService(
context,
NotificationManager::class.java
) as NotificationManager

notificationManager.sendNotification(
context.getText(R.string.notification_msg).toString(),
context
)

}

}
class SnoozeReceiver: BroadcastReceiver() {
private val REQUEST_CODE = 0

override fun onReceive(context: Context, intent: Intent) {
val triggerTime = SystemClock.elapsedRealtime() + DateUtils.MINUTE_IN_MILLIS // Time after which you want to show the //notification again

val notifyIntent = Intent(context, AlarmReceiver::class.java)
val notifyPendingIntent = PendingIntent.getBroadcast(
context,
REQUEST_CODE,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
AlarmManagerCompat.setExactAndAllowWhileIdle(
alarmManager,
AlarmManager.ELAPSED_REALTIME_WAKEUP,
triggerTime,
notifyPendingIntent
)

val notificationManager = ContextCompat.getSystemService(
context,
NotificationManager::class.java
) as NotificationManager
notificationManager.cancelAll()
}

}
<receiver
android:name=".receiver.AlarmReceiver"
android:enabled="true"
android:exported="false">
</receiver>
<receiver
android:name=".receiver.SnoozeReceiver"
android:enabled="true"
android:exported="false">
</receiver>
private val NOTIFICATION_ID = 0
private val REQUEST_CODE = 0
private val FLAGS = 0
fun NotificationManager.sendNotification(messageBody: String, applicationContext: Context) {
// Create the content intent for the notification which launches the MainActivity

val contentIntent = Intent(applicationContext, MainActivity::class.java)
val contentPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_ID,
contentIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)

val largeImage = BitmapFactory.decodeResource(
applicationContext.resources,
R.drawable.large_image
)
val bigPicStyle = NotificationCompat.BigPictureStyle()
.bigPicture(largeImage)
.bigLargeIcon(null)

// Add snooze action
val snoozeIntent = Intent(applicationContext, SnoozeReceiver::class.java)
val snoozePendingIntent: PendingIntent = PendingIntent.getBroadcast(
applicationContext,
REQUEST_CODE,
snoozeIntent,
FLAGS)
val builder = NotificationCompat.Builder(
applicationContext,
applicationContext.getString(R.string.notification_channel_id)
)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(applicationContext
.getString(R.string.notification_title))
.setContentText(messageBody)
.setContentIntent(contentPendingIntent)
.setAutoCancel(true)
.setStyle(bigPicStyle)
.setLargeIcon(eggImage)
.addAction(
R.drawable.button_icon,
applicationContext.getString(R.string.snooze),
snoozePendingIntent
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
notify(NOTIFICATION_ID, builder.build())
}

/**
* Cancels all notifications.
*
*/
fun NotificationManager.cancelNotifications() {
cancelAll()
}
val notificationManager =
ContextCompat.getSystemService(
app,
NotificationManager::class.java
) as NotificationManager
notificationManager.cancelNotifications()
val notificationManager =
ContextCompat.getSystemService(
app,
NotificationManager::class.java
) as NotificationManager
notificationManager.sendNotification(
context.getText(R.string.notification_msg).toString(),
context
)
val notifyIntent = Intent(context, AlarmReceiver::class.java)
val notifyPendingIntent = PendingIntent.getBroadcast(
context,
REQUEST_CODE,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
AlarmManagerCompat.setExactAndAllowWhileIdle(
alarmManager,
AlarmManager.ELAPSED_REALTIME_WAKEUP,
triggerTime,
notifyPendingIntent
)

Android Developer @BluSmart

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Software Complexity 1: Black Boxes And Messages

Software, in a nutshell

Humans of CodeChem: Martina Papaliska

Cloudways : One platform to choose cloud based hosting solution.

Cometh Mission v0.2 🚀

Recursive File and Directory, Environment Variables, Excel File in Python

Beginners guide to gateways and proxies

Let’s find Answers to Common Objections about TDD

5 Reasons Why Apache Kafka Needs a Distributed SQL Database

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Himanshu Choudhary

Himanshu Choudhary

Android Developer @BluSmart

More from Medium

Bypassing Android 12 SplashScreen API

Notification Alert! Integration of Huawei Push Kit in Application (Kotlin)

Local Databases in Android

Android: Compose UI Screenshot Testing with Firebase Test Lab and CircleCI