LocalNotificationCenter
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
需求
安装
LocalNotificationCenter 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile
pod 'LocalNotificationCenter'
使用说明
LocalNotificationCenter 是围绕 UserNotifications 框架的一个轻量级封装,可以用来轻松地在 iOS 上安排和管理本地通知。
通知上下文
LocalNotificationCenter 的每个实例都维护自己的上下文。默认的通知中心使用默认上下文,但也可以创建具有不同上下文的实例。
在 LocalNotificationCenter 实例中执行的所有操作仅与其自己的上下文相关。
/// Scoped to the default context.
let defaultNotificationCenter = LocalNotificationCenter.default
/// Scoped to a unique context.
let uniqueNotificationCenter = LocalNotificationCenter(context: "my_unique_context")
安排本地通知
// create a unique identifier for this notification (i.e. some database id)
let identifier = <Some Unique Identifier>
// repeat monthly on the 24th day, at 8:30am
let date = DateComponents()
date.day = 24
date.hour = 8
date.minutes = 30
// schedule a new one
LocalNotificationCenter.default.scheduleLocalNotification(withIdentifier: identifier,
body: message,
dateMatching: date,
repeats: true)
取消单个挂起的本地通知
您可以通过其唯一的标识符取消之前安排的通知。
LocalNotificationCenter.default.cancelScheduledLocalNotification(forIdentifier: identifier)
取消所有挂起的本地通知
在某些情况下,您可能想取消上下文中之前安排的所有通知。
LocalNotificationCenter.default.cancelAllScheduledLocalNotifications()
作者
cgossain, [email protected]
许可协议
LocalNotificationCenter 遵循 MIT 许可协议。有关更多信息,请参见 LICENSE 文件。