Robin 0.98.0

Robin 0.98.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2021年3月
SPM支持 SPM

Ahmed 维护。



Robin 0.98.0

  • 作者
  • Ahmed Mohamed

Robin

Platform Version Swift 4+ CI Status License Release

Robin 是一个用于 iOS 和 macOS 的通知界面,它可以后台处理 UserNotifications。

要求

  • iOS 10.0+
  • macOS 10.14+
  • Xcode 10.0+
  • Swift 4.2+

通讯

  • 如果您需要帮助或有问题,请在 Stack Overflow 上使用 'robin' 标签。
  • 如果您发现了一个错误或有一个功能请求,请提交一个问题。

在发布贡献指南之前,请不要提交补丁请求。

安装

Robin 通过 Swift Package ManagerCocoaPods 提供。

使用 SPM 安装

.package(url: "https://github.com/ahmdx/Robin", from: "0.93.0"),

CocoaPods

pod 'Robin', '~> 0.93.0'

如果您想将测试套件包含在项目中

pod 'Robin', '~> 0.93.0', :testspecs => ['Tests']

使用方法

import Robin

在使用 Robin 之前,您需要请求发送通知给用户的权限。以下请求 badgesoundalert 权限。有关所有可用选项,请参阅 UNAuthorizationOptions

Robin.settings.requestAuthorization(forOptions: [.badge, .sound, .alert]) { grant, error in
  // Handle authorization or error
}

设置

要查询应用程序的通知设置,请使用 Robin.settings

let alertStyle = Robin.settings.alertStyle
let authorizationStatus = Robin.settings.authorizationStatus
let enabledSettings = Robin.settings.enabledSettings

这返回了一个所有启用设置的选项集。如果某些设置未包括在该集合中,它们可能已禁用或不受支持。如果您想了解某些特定设置是否启用,例如,您可以使用 enabledSettings.contains(.sound)。有关更多详情,请参阅 RobinSettingsOptions

let showPreviews = Robin.settings.showPreviews

当应用程序从非活动状态变为活动状态时,Robin 会自动更新应用程序设置的详细信息,以避免不必要的查询。如果想要覆盖此行为并手动更新信息,您可以使用 forceRefresh()

Robin.settings.forceRefresh()

通知

通过操作 RobinNotification 对象来使用 Robin 接管 iOS 通知的安排。要创建一个 RobinNotification 对象,只需调用它的初始化器。

init(identifier: String = default, body: String, date: Date = default)

以下是一个例子:一个具有唯一标识符的通知,将在一小时后触发。

let notification = RobinNotification(body: "A notification", date: Date().next(hours: 1))

next(minutes:)next(hours:)next(days:) 都是 Date 扩展的一部分。

下表总结了所有 RobinNotification 属性。

属性 类型 描述
badge NSNumber? 通知应在应用图标上显示的数字。
body String! 通知的主体字符串。
date Date! 设置通知触发的时间。
identifier[1] String! 分配给通知的字符串,供以后访问。
repeats Repeats 通知的重复间隔。其中之一是 none(默认)、hourdayweekmonth
scheduled Bool 通知的状态。只读
delivered Bool 通知的投递状态。只读
sound RobinNotificationSound 通知的声音名称。
title String? 通知的标题字符串。
userInfo[2] 任何可哈希的任何类型不透明 closures! 一个包含额外信息的字典。

[1] identifierRobinNotification 初始化后为只读。

[2] 要在 userInfo 中添加和移除键,请分别使用 setUserInfo(value: Any, forKey: AnyHashable)removeUserInfoValue(forKey: AnyHashable)

安排一个通知

创建一个 RobinNotification 对象后,可以使用 schedule(notification:) 来安排它。

let scheduledNotification = Robin.scheduler.schedule(notification: notification)

如果成功安排,则 scheduledNotification 是一个有效的 RobinNotification 对象,否则为 nil

检索一个通知

通过调用 notification(withIdentifier: String) -> RobinNotification? 简单地检索已安排的通知。

let scheduledNotification = Robin.scheduler.notification(withIdentifier: "identifier")

取消一个通知

要取消通知,可以调用 cancel(notification: RobinNotification)cancel(withIdentifier: String)

Robin.scheduler.cancel(notification: scheduledNotification)
Robin.scheduler.cancel(withIdentifier: scheduledNotification.identifier)

Robin 允许您通过调用 cancelAll() 来取消所有已安排的通知。

Robin.scheduler.cancelAll()

检索所有已递送的通知

要检索在通知中心显示的所有已递送通知,请调用 allDelivered(completionHandler: @escaping ([RobinNotification]) -> Void)

Robin.manager.allDelivered { deliveredNotifications in
  // Access delivered notifications
}

移除一个已递送的通知

要从通知中心移除已递送的通知,可以调用 removeDelivered(notification: RobinNotification)removeDelivered(withIdentifier identifier: String)

Robin.manager.removeDelivered(notification: deliveredNotification)
Robin.manager.removeDelivered(withIdentifier: deliveredNotification.identifier)

Robin 允许您通过调用 removeAllDelivered() 移除所有已送达的通知

Robin.manager.removeAllDelivered()

说明

Robin 默认设置为允许 iOS 安排 60 个通知。剩下的四个插槽留作应用程序定义的通知。这些空闲插槽目前由 Robin 不处理;如果您使用 Robin 利用这些插槽,通知将被丢弃。要更改允许的最大数量,只需更新 Robin.maximumAllowedNotifications

Robin 目前无法处理具有相同标识符的多个通知。针对同一标识符下分组通知的支持即将推出。

作者

Ahmed Mohamed,[email protected]

许可证

Robin 采用 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。