Skip to content

Instantly share code, notes, and snippets.

@garg-lucifer
Last active Sep 24, 2022
Embed
What would you like to do?
class AlarmReceiver : BroadcastReceiver() {
private var notificationManager: NotificationManagerCompat? = null
override fun onReceive(p0: Context?, p1: Intent?) {
val taskInfo = p1?.getSerializableExtra("task_info") as? TaskInfo
val tapResultIntent = Intent(p0, MainActivity::class.java)
tapResultIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
val pendingIntent: PendingIntent = getActivity( p0,0,tapResultIntent,FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
// creating intent and pending intent to open OnCompletedBroadcastReceiver class
val intentOnCompleted = Intent(p0, OnCompletedBroadcastReceiver::class.java).apply {
putExtra("task_info", taskInfo)
}
val pendingIntentOnCompleted: PendingIntent? =
taskInfo?.let { getBroadcast(p0, it.id,intentOnCompleted,FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE) }
// creating the action button
val actionCompleted : NotificationCompat.Action = NotificationCompat.Action.Builder(0,"Completed",pendingIntentOnCompleted).build()
val notification = p0?.let {
NotificationCompat.Builder(it, "to_do_list")
.setContentTitle("Task Reminder")
.setContentText(taskInfo?.description)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.addAction(actionCompleted) // adding the action button to notification
.build()
}
notificationManager = p0?.let { NotificationManagerCompat.from(it) }
notification?.let { taskInfo?.let { it1 -> notificationManager?.notify(it1.id, it) } }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment