ABNScheduler 是用 Swift 编写的 iOS 本地通知调度器。
只需将 ABNScheduler.swift
复制到您的项目中,即可开始使用。
var notification = ABNotification(alertBody: "A Notification")
let identifier = notification.schedule(fireDate: NSDate().nextHours(2))
schedule(fireDate:)
如果成功调度,则返回通知的标识符。如果未调度或已经被调度,则返回 nil
。
为了给通知预设一个标识符
var notification = ABNotification(alertBody: "A Notification", identifier: "identifier")
let identifier = notification.schedule(fireDate: NSDate().nextHours(2))
nextHours(_: 是一个 NSObject 类扩展的一部分。
您还可以快速调度一个通知
let identifier = ABNScheduler.schedule(alertBody: "A Notification", fireDate: NSDate().nextDays(1))
nextDays(_: 是一个是 NSObject 类扩展的一部分。
为今天或明天的特定时间调度通知
let identifier = ABNScheduler.schedule(alertBody: "A Notification", fireDate: 1300.date)
如果当前时间在下午 1:00 之前,则此通知将调度在今天下午 1:00,否则将调度在明天下午 1:00。
date 是一个计算属性,是 Int 扩展的一部分。
ABNScheduler 使用优先级队列来处理超过 64 个通知。当应用程序不能再调度更多通知时,它们将被添加到应用程序的通知队列中。这就是为什么您需要在某些已计划的通知被触发时调用 ABNScheduler.scheduleNotificationsFromQueue()
的原因。
另外,通知队列不会自动保存到磁盘,因此您需要在使用时调用 ABNScheduler.saveQueue()
以将队列持久保存到磁盘。当应用程序启动时,不需要加载队列;这是在需要访问队列时自动处理的。
###重复通知
您可以使用 Repeats
的 .Hourly
、.Daily
、.Weekly
、.Monthly
、.Yearly
分别以每小时、每天、每周、每月、每年重复一个通知。
每天重复通知
var notification = ABNotification(alertBody: "A Notification")
notification.repeatInterval = Repeats.Daily
let identifier = notification.schedule(fireDate: NSDate().nextHours(2))
let notification = ABNScheduler.notificationWithIdentifier("identifier")
要通过 iOS 检索计划中的通知,请调用 ABNScheduler.scheduledNotifications()
。这将返回一个 ABNotification 数组。
let notification = ABNScheduler.notificationWithIdentifier("identifier")
notification?.reschedule(fireDate: NSDate().nextMinutes(10))
nextMinutes(_: 是一个 NSObject 类扩展的一部分。
您还可以使用以下方式快速摇铃通知数分钟、数小时或数天使用
snoozeForMinutes(_:)
、snoozeForHours(_:)
或 分别。
如果您觉得通知组织得不好,您可以调用 ABNScheduler.rescheduleNotifications()
。这将重新排列 iOS 和队列中存储的计划中的通知。这可能在应用程序启动时非常有用。
let notification = ABNScheduler.notificationWithIdentifier("identifier")
ABNScheduler.cancel(notification!)
let notification = ABNScheduler.notificationWithIdentifier("identifier")
notification?.cancel()
您可以通过调用 ABNScheduler.cancelAllNotifications()
来取消所有已计划的提醒。
由于额外的提醒完全由应用处理,iOS 调度提醒时必须启动应用。只需别忘了调用 ABNScheduler.scheduleNotificationsFromQueue()
。
当前版本 ABNScheduler 不支持处理具有相同标识符的多个提醒。已提交为问题,并将尽快完成。
调度器默认设置为允许 iOS 计划 60 个提醒。剩余的四个槽位保留给不需要排队的应用定义提醒。这些空闲槽位目前由 ABNScheduler 未处理;如果您使用 ABNScheduler 利用这些槽位,提醒将被添加到队列中。要更改最大限制,只需更新 ABNScheduler.swift 中的 maximumScheduledNotifications
。
在 AppDelegate 类的 application(_:didReceiveLocalNotification:)
中或在应用启动时调用 ABNScheduler.scheduleNotificationsFromQueue()
是一个好习惯。如果您打算在应用启动时调用 ABNScheduler.rescheduleNotifications()
,则无需调用 ABNScheduler.scheduleNotificationsFromQueue()
。
您可以使用 UILocalNotificaion 作为参数创建 ABNotification 实例,但是请确保这个 UILocalNotificaion 的用户信息字典中有一个 ABNIdentifier
键。
ABNScheduler 由 Ahmed Abdul Badie 编写。您完全可以向我提交问题和拉取请求。欢迎通过我提供的 GitHub 页面邮箱联系我。
ABNScheduler 在 MIT 许可证下发布。更多详情,请参阅 LICENSE。