Skip to content

Instantly share code, notes, and snippets.

@PatilShreyas
Created December 13, 2019 08:02
Show Gist options
  • Select an option

  • Save PatilShreyas/0d8472b5dee7dd5d2ba0e7714f82c8f7 to your computer and use it in GitHub Desktop.

Select an option

Save PatilShreyas/0d8472b5dee7dd5d2ba0e7714f82c8f7 to your computer and use it in GitHub Desktop.
private fun scheduleAlarm(
scheduledTimeString: String?,
title: String?,
message: String?
) {
val alarmMgr = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val alarmIntent =
Intent(applicationContext, NotificationBroadcastReceiver::class.java).let { intent ->
intent.putExtra(NOTIFICATION_TITLE, title)
intent.putExtra(NOTIFICATION_MESSAGE, message)
PendingIntent.getBroadcast(applicationContext, 0, intent, 0)
}
// Parse Schedule time
val scheduledTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
.parse(scheduledTimeString!!)
scheduledTime?.let {
// With set(), it'll set non repeating one time alarm.
alarmMgr.set(
AlarmManager.RTC_WAKEUP,
it.time,
alarmIntent
)
}
}
private fun showNotification(title: String, message: String) {
NotificationUtil(applicationContext).showNotification(title, message)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment