如何从特定的未来日期开始每两周触发一次本地通知

Arn*_*nab 1 ios localnotification ios10 unusernotificationcenter

我试图UNUserNotification每两周触发一次,这将从未来的特定一天开始。我已经成功触发了UNTimeIntervalNotificationTrigger。但我的问题是我无法在这里设置具体的开始日期。

UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:(14*24*3600) repeats: YES];
request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
Run Code Online (Sandbox Code Playgroud)

我已经尝试过WeekdayOrdinalUNCalendarNotificationTrigger但这并不总是适用于两周的时间。

有什么办法可以UNUserNotification从未来的特定日期开始每两周安排一次本地活动吗?

小智 6

UNNotificationRequest不提供任何触发两周重复的内容。

UNCalendarNotificationTrigger只能触发以下类型的重复:

weekly, daily, monthly,yearly

switch repeatType {
    case .daily:
        newComponents.hour = components.hour
        newComponents.minute = components.minute
        break
    case .weekly:
        newComponents.hour = components.hour
        newComponents.minute = components.minute
        newComponents.weekday = components.weekday
        break
    case .monthly:
        newComponents.hour = components.hour
        newComponents.minute = components.minute
        newComponents.day = components.day
        break
    case .none:
        newComponents.hour = components.hour
        newComponents.minute = components.minute
        newComponents.day = components.day
        newComponents.month = components.month
        newComponents.year = components.year
        break
    case .annually:
        newComponents.hour = components.hour
        newComponents.minute = components.minute
        newComponents.day = components.day
        newComponents.month = components.month
        break
    }
Run Code Online (Sandbox Code Playgroud)

尽管您可以轻松地使用EventKit您想要的所有此类重复选项(如两周、每季度、半年等)在这些事件中创建事件和警报。

我创建了此代码来创建具有多种重复类型的事件:

let eventStore = EKEventStore()
    eventStore.requestAccess(to: .event, completion: { (granted, error) in
        if (granted) && (error == nil) {
            let event = EKEvent(eventStore: eventStore)
            event.title = "Countdown title"
            event.startDate = Date()
            event.notes = "Countdown is complete!"
            event.isAllDay = false

            event.calendar = eventStore.defaultCalendarForNewEvents

            var frequency : EKRecurrenceFrequency = EKRecurrenceFrequency.daily
            var interval = 1

            switch repeatType {
            case .daily:
                //Repeat every day
                frequency = EKRecurrenceFrequency.daily
                interval = 1
                break
            case .weekly:
                //Repeat every week
                frequency = EKRecurrenceFrequency.weekly
                interval = 1
                break
            case .biweekly:
                //Repeat every 2 weeks
                frequency = EKRecurrenceFrequency.weekly
                interval = 2
                break
            case .monthly:
                //Repeat every month
                frequency = EKRecurrenceFrequency.monthly
                interval = 1
                break
            case .quarterly:
                //Repeat every 3 months
                frequency = EKRecurrenceFrequency.monthly
                interval = 3
                break
            case .halfYearly:
                //Repeat every 6 months
                frequency = EKRecurrenceFrequency.monthly
                interval = 6
                break
            default:
                // Repeat every year
                frequency = EKRecurrenceFrequency.yearly
                interval = 1

            }
            let alarm : EKAlarm = EKAlarm(relativeOffset: TimeInterval(exactly: 0)!)
            event.addAlarm(alarm)

            if interval > 0 {
                event.addRecurrenceRule(EKRecurrenceRule(recurrenceWith: frequency, interval: interval, end: nil))
            }

            do {
                try eventStore.save(event, span: .thisEvent)
            } catch let error as NSError {
                print(error.localizedDescription)
                return
            }
        } else {
            print(error?.localizedDescription ?? "no error")
        }
    })
Run Code Online (Sandbox Code Playgroud)

请不要忘记导入EventKit