对于iOS 10,请在每周的特定日期重复UserNotification

时间:2016-12-26 11:10:54

标签: swift ios10 usernotifications

我正在研究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)")
   }
})

更新

我也发现在该日期通知被解雇后,检查是否存在更多未决通知或检查是否已重新安排该通知。实际上重复等于真,它还没有安排在下周再次安排。

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中的错误。

2 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:1)

这是应该有效的方法:

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

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