Created
May 2, 2024 22:40
-
-
Save igorescodro/98ce7576cc0f2f1522b574d77715dd54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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