I am working on a local notification planning module for iOS 10 that repeats local notification, for example, every Sunday or every Monday. Suppose I set a notification for this date 2016-12-27 10:53:22 +0000
and using UNCalendarNotificationTrigger
, where the repeat value is true, the notification is triggered for those that were on this date, and it does not repeat the next week in one and same time.
What could be the reason for this? and how you can repeat every week for a specific day in iOS 10.
Here is the code for creating a local notification:
let content = UNMutableNotificationContent() content.title = object.title content.body = object.body content.sound = UNNotificationSound.default() let date = object.fireDate let triggerDate = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: date as Date) let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true) // Swift let identifier = object.id let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger) localNotification.add(request, withCompletionHandler: { (error) in if error != nil { // Something went wrong print(error!) }else { print("Reminder \(object.id) has been added successfully at \(object.fireDate)") } })
Update:
I also found that after the notice was fired on that date and to check whether there was a pending notification notice, or to check whether it was rescheduled or not. in fact with a repeat of true, it was not scheduled for next week.
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (notficiations) in for localNotification in notficiations { print(localNotification) } })
And the result:
<UNNotificationRequest: 0x174223ca0; identifier: A1, content: <UNNotificationContent: 0x1742e2980; title: My Title, subtitle: (null), body: My Body, categoryIdentifier: , launchImageName: , peopleIdentifiers: ( ), threadIdentifier: , attachments: ( ), badge: (null), sound: <UNNotificationSound: 0x1740b1820>, hasDefaultAction: YES, shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNCalendarNotificationTrigger: 0x174223cc0; dateComponents: <NSDateComponents: 0x17415e140> Calendar Year: 2016 Month: 12 Day: 27 Hour: 14 Minute: 46 Second: 15, repeats: YES>>
I don't know if this is really a bug in iOS or not.