Skip to content

Instantly share code, notes, and snippets.

@garg-lucifer
Last active September 24, 2022 07:52
Embed
What would you like to do?
private fun setAlarm(taskInfo: TaskInfo) {
// creating alarmManager instance
val alarmManager = activity?.getSystemService(Context.ALARM_SERVICE) as AlarmManager
// adding intent and pending intent to go to AlarmReceiver Class in future
val intent = Intent(requireContext(), AlarmReceiver::class.java)
intent.putExtra("task_info", taskInfo)
val pendingIntent = PendingIntent.getBroadcast(requireContext(), taskInfo.id, intent, PendingIntent.FLAG_IMMUTABLE)
// when using setAlarmClock() it displays a notification until alarm rings and when pressed it takes us to mainActivity
val mainActivityIntent = Intent(requireContext(), MainActivity::class.java)
val basicPendingIntent = PendingIntent.getActivity(requireContext(), taskInfo.id, mainActivityIntent, PendingIntent.FLAG_IMMUTABLE)
// creating clockInfo instance
val clockInfo = AlarmManager.AlarmClockInfo(taskInfo.date.time, basicPendingIntent)
// setting the alarm
alarmManager.setAlarmClock(clockInfo, pendingIntent)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment