2017-04-07 63 views
0

我已經創建了不同的本地通知,爲工作日和週末設置爲重複不斷如何取消特定重複本地通知,並重新計劃下週在iOS的10(SWIFT)

let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true) 
let content = UNMutableNotificationContent() 
content.body = "TIME_TO_STEP_SHAPA".localized 
content.sound = UNNotificationSound.default() 

let request = UNNotificationRequest(identifier: "\(notificationType)-\(reminderType)-\(day)", content: content, trigger: trigger) 

UNUserNotificationCenter.current().getNotificationSettings { (settings) in 
    if settings.authorizationStatus != .authorized { 
     print("Local notifications are not authorized by the user") 
    } 
} 
UNUserNotificationCenter.current().add(request) {(error) in 
    if error != nil { 
     print("Error: \(error?.localizedDescription)") 
    } 
} 

和取消特別通知,確認基於適當條件的通知並在下週更新相同的通知。

if (type == notificationType && notificationWeekDay == currentWeekDay) { 
    //Cancelling local notification 
    app.cancelLocalNotification(notif) 

    let fireDate = self.getUpdatedNotification(currentDate: currentLocalDate!, fireDate: notificationFireDate!) 

    HikeCommonUtils.setUpLocalNotification(fireDate, type: notificationType, reminderType: reminderType) 
} 

和即使在拆除通知解僱,因爲重複的「真」的刪除日期後更新使用

func getUpdatedNotification(currentDate: Date, fireDate : Date) ->Date { 
    let calendar = Calendar.autoupdatingCurrent 
    let dateComponents = (calendar as NSCalendar).components(([.year, .month, .day]), from: currentDate) 
    let timeComponents = (calendar as NSCalendar).components(([.hour, .minute, .second]), from: fireDate) 
    var dateComps = DateComponents() 
    dateComps.day = dateComponents.day! + 7 
    dateComps.month = dateComponents.month 
    dateComps.year = dateComponents.year 
    dateComps.hour = timeComponents.hour 
    dateComps.minute = timeComponents.minute 
    dateComps.second = timeComponents.second 
    let itemDate = calendar.date(from: dateComps) 
    return itemDate! 
} 

下一個火日期。

是否有任何選擇在iOS 10的本地通知中添加開始日期?

在此先感謝!

回答

0

更新與

var triggerWeekly = calendar.dateComponents([.hour, .minute, .second], from: fireDate) 
     triggerWeekly.weekday = day 

而不是

  let triggerWeekly = calendar.dateComponents([.weekday, .hour, .minute, .second], from: fireDate) 

通知觸發細節就職於firedate更新

0

您可以添加邏輯來nextTriggerDate()

func nextTriggerDate() 

在其觸發條件將得到滿足後一個日期。

討論

使用這個屬性來找出何時與此觸發器關聯的通知接下來將交付。