【问题标题】:Repeat UserNotification every specific day of week for iOS 10为 iOS 10 每周特定的每一天重复 UserNotification
【发布时间】:2016-12-26 11:10:53
【问题描述】:

我正在为 iOS 10 开发本地通知计划模块,该模块会重复本地通知,例如每周日或每周一等。假设我为此日期安排了一个通知 2016-12-27 10:53:22 +0000 并使用 UNCalendarNotificationTrigger 的重复值等于 true,通知会在该日期触发,并且不会在下周同时重复。

这可能是什么原因?以及如何在 iOS 10 中每周重复特定的一天。

这里是创建本地通知的代码:

 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)")
   }
})

更新:

我还发现,在通知在该日期被触发后,并检查是否没有更多待处理的通知,或者检查它是否已重新安排。实际上,repeat 等于 true,它还没有被安排在下周。

UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (notficiations) in

            for localNotification in notficiations {

                print(localNotification)

            }
        })

结果是:

<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>>

我不知道它是否真的是 iOS 中的错误。

【问题讨论】:

    标签: swift ios10 usernotifications


    【解决方案1】:

    触发日期格式不适用于在一周中的某一天重复通知。 您当前的触发日期组件包括年、月、日等,因此此通知在每年的特定月份和日期重复。如下所述更改触发日期以在一周中的某一天重复通知。

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

    【讨论】:

      【解决方案2】:

      这是应该可行的方法:

      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
              }
          })
      }
      

      基本上,我发现您可以为希望触发警报的每一天设置单独的通知。例如,您想要每个星期一和每个星期二,因此为每一天创建一个日期组件并将其添加到触发器中。它并不完美,但我认为它比计算时间间隔更好。

      【讨论】:

      • 一些解释会显着提高你的回答质量
      猜你喜欢
      • 2016-11-17
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 2017-09-26
      • 2012-01-26
      相关资源
      最近更新 更多