Repeat UserNotification every specific day of the week for iOS 10

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.

+6
source share
2 answers

The launch date format is not suitable for re-notification during the day of the week. Your current launch date components include year, month, day, etc. Therefore, this notification is repeated every year in that specific month and day. Select a trigger date as follows to repeat the notification during the day of the week.

  let triggerDate = Calendar.current.dateComponents([.weekday,.hour,.minute], from: date as Date) 
+6
source

Here is an approach that should work:

 func addNotificationForAlarm(alarm: MyAlarm) { let myAlarmNotifContent = UNMutableNotificationContent() myAlarmNotifContent.title = "Reminder" myAlarmNotifContent.body = alarm.activity myAlarmNotifContent.sound = UNNotificationSound.default() if alarm.repeatDays.count == 1 { } else { for index in 1...alarm.repeatDays.count { createNotif(date: alarm.date, weekDay: index, content: myAlarmNotifContent) } } } private func createNotif(date: Date, weekDay: Int, content: UNNotificationContent) { var dateComponent = DateComponents() dateComponent.weekday = weekDay dateComponent.hour = Calendar.current.dateComponents([.hour], from: date).hashValue dateComponent.minute = Calendar.current.dateComponents([.minute], from: date).hashValue let myAlarmTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true) setupNotificationSettings() let identifier = "Your-Notification" let request = UNNotificationRequest(identifier: identifier, content: content, trigger: myAlarmTrigger) let center = UNUserNotificationCenter.current() center.add(request, withCompletionHandler: { (error) in if error != nil { //TODO: Handle the error } }) } 

Basically, I found that you can set a separate notification for each day when you want to call an alarm. For example, you want every Monday and every Tuesday, so create a date component for each day and add it to the trigger. This is not ideal, but it is a solution that, I think, is better than calculating time intervals.

0
source

Source: https://habr.com/ru/post/1013521/


All Articles