2016-12-26 164 views
6

我正在爲iOS 10的本地通知調度模塊,例如每週日或每週一重複本地通知..等等。比方說,我預定了這個日期的通知2016-12-27 10:53:22 +0000,並使用UNCalendarNotificationTrigger,重複值等於true,通知將在該日期觸發,並且不會在下週同時重複。重複用戶通知iOS 10的每個特定的一天

這可能是什麼原因?而且怎麼可能每週重複特定的一天,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中的錯誤或不。

回答

6

觸發日期格式不適合在一週中的某天重複通知。 您當前的觸發日期組件包括年,月,日等,因此此通知在該特定的月份和日期中每年重複一次。更改如下所述的觸發日期以在一週中的某一天重複通知。

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

這裏的做法,應該工作:

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

基本上我所發現的是,你可以設置你想要的報警觸發每天分別安排一個通知。例如,您希望每個星期一和每個星期二都創建一個日期組件,並將其添加到觸發器中。這並不完美,但是我認爲這個解決方案比計算時間間隔更好。

+0

一些解釋會顯着提高你的答案的質量 – mrun

相關問題