diff --git a/.changes/fix-notification-schedule-ios.md b/.changes/fix-notification-schedule-ios.md new file mode 100644 index 0000000000..481cba1b6f --- /dev/null +++ b/.changes/fix-notification-schedule-ios.md @@ -0,0 +1,6 @@ +--- +"notification": patch +"notification-js": patch +--- + +Fix notification scheduling on iOS. diff --git a/plugins/notification/ios/Sources/Notification.swift b/plugins/notification/ios/Sources/Notification.swift index adba05ec25..259399b8d4 100644 --- a/plugins/notification/ios/Sources/Notification.swift +++ b/plugins/notification/ios/Sources/Notification.swift @@ -38,10 +38,17 @@ func makeNotificationContent(_ notification: Notification) throws -> UNNotificat arguments: nil) } - content.userInfo = [ - "__EXTRA__": notification.extra as Any, - "__SCHEDULE__": notification.schedule as Any, - ] + var userInfo: [String: Any] = [:] + + if let extra = notification.extra { + userInfo["__EXTRA__"] = extra + } + + if let schedule = notification.schedule { + userInfo["__SCHEDULE__"] = scheduleToDictionary(schedule) + } + + content.userInfo = userInfo if let actionTypeId = notification.actionTypeId { content.categoryIdentifier = actionTypeId @@ -66,6 +73,56 @@ func makeNotificationContent(_ notification: Notification) throws -> UNNotificat return content } +func scheduleToDictionary(_ schedule: NotificationSchedule) -> [String: Any] { + switch schedule { + case .at(let date, let repeating): + return [ + "type": "at", + "date": date, + "repeating": repeating + ] + case .interval(let interval): + return [ + "type": "interval", + "interval": scheduleIntervalToDictionary(interval) + ] + case .every(let interval, let count): + return [ + "type": "every", + "interval": interval.rawValue, + "count": count + ] + } +} + +func scheduleIntervalToDictionary(_ interval: ScheduleInterval) -> [String: Any] { + var dict: [String: Any] = [:] + + if let year = interval.year { + dict["year"] = year + } + if let month = interval.month { + dict["month"] = month + } + if let day = interval.day { + dict["day"] = day + } + if let weekday = interval.weekday { + dict["weekday"] = weekday + } + if let hour = interval.hour { + dict["hour"] = hour + } + if let minute = interval.minute { + dict["minute"] = minute + } + if let second = interval.second { + dict["second"] = second + } + + return dict +} + func makeAttachments(_ attachments: [NotificationAttachment]) throws -> [UNNotificationAttachment] { var createdAttachments = [UNNotificationAttachment]()