【问题标题】:How to cancel particular recurring local notification and reschedule it for next week in iOS 10(Swift)如何在 iOS 10(Swift)中取消特定的定期本地通知并将其重新安排到下周
【发布时间】:2017-04-07 14:01:23
【问题描述】:

我创建了不同的本地通知来设置工作日和周末连续重复

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 的本地通知中是否有添加开始日期的选项?

提前致谢!!

【问题讨论】:

    标签: swift3 ios10 unusernotificationcenter


    【解决方案1】:

    更新通知触发器详细信息

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

    代替

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

    为触发更新工作

    【讨论】:

      【解决方案2】:

      你可以给nextTriggerDate()添加逻辑

      func nextTriggerDate()
      

      满足触发条件的下一个日期。

      讨论

      使用此属性可确定下一次发送与此触发器关联的通知的时间。

      【讨论】:

        猜你喜欢
        • 2017-12-17
        • 2017-04-12
        • 1970-01-01
        • 1970-01-01
        • 2022-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多