Skip to content

Instantly share code, notes, and snippets.

Last active September 24, 2022 07:52
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(),
intent.putExtra("task_info", taskInfo)
val pendingIntent = PendingIntent.getBroadcast(requireContext(),, 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(),
val basicPendingIntent = PendingIntent.getActivity(requireContext(),, mainActivityIntent, PendingIntent.FLAG_IMMUTABLE)
// creating clockInfo instance
val clockInfo = AlarmManager.AlarmClockInfo(, 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