Skip to content

Instantly share code, notes, and snippets.

@igorescodro
Created May 2, 2024 22:40
Show Gist options
  • Save igorescodro/98ce7576cc0f2f1522b574d77715dd54 to your computer and use it in GitHub Desktop.
Save igorescodro/98ce7576cc0f2f1522b574d77715dd54 to your computer and use it in GitHub Desktop.
override fun scheduleTaskNotification(task: Task, timeInMillis: Long) {
val content = UNMutableNotificationContent()
content.setBody(task.title)
content.setCategoryIdentifier(CATEGORY_IDENTIFIER_TASK)
content.setUserInfo(mapOf(USER_INFO_TASK_ID to task.id))
val nsDate = NSDate.dateWithTimeIntervalSince1970(timeInMillis / 1000.0)
val dateComponents = NSCalendar.currentCalendar.components(
NSCalendarUnitYear or NSCalendarUnitMonth or NSCalendarUnitDay
or NSCalendarUnitHour or NSCalendarUnitMinute,
fromDate = nsDate,
)
val trigger = UNCalendarNotificationTrigger.triggerWithDateMatchingComponents(
dateComponents = dateComponents,
repeats = false,
)
val request = UNNotificationRequest.requestWithIdentifier(
identifier = task.id.toString(),
content = content,
trigger = trigger,
)
NSLog("Scheduling notification for '${task.title}' at '$timeInMillis'")
val notificationCenter = UNUserNotificationCenter.currentNotificationCenter()
notificationCenter.addNotificationRequest(request) { error ->
if (error != null) {
NSLog("Error scheduling notification: $error")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment