Skip to content

Instantly share code, notes, and snippets.

@garg-lucifer
Created September 23, 2022 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garg-lucifer/02833f8292bd625f1df5f002e6caa2e0 to your computer and use it in GitHub Desktop.
Save garg-lucifer/02833f8292bd625f1df5f002e6caa2e0 to your computer and use it in GitHub Desktop.
class AlarmReceiver : BroadcastReceiver() {
private var notificationManager: NotificationManagerCompat? = null
override fun onReceive(p0: Context?, p1: Intent?) {
val taskInfo = p1?.getSerializableExtra("task_info") as? TaskInfo
// tapResultIntent gets executed when user taps the notification
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)
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)
.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