SwiftNotification
内容
要求
- iOS 10.0+
- Xcode 8.0+
- Swift 3.0+
安装
SwiftNotification 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'SwiftNotification'
end
然后运行以下命令:
$ pod install
使用
要管理您自定义的通知,您可以添加如下代码:
AppDelegate
/**
AppDelegate.
*/
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
/**
Setup SwiftNotification
*/
// Set your center such as UNUserNotificationCenter.current()
NotificationSettings.center = '<Your Current Center>'
// Set your custom center delegate such as UNUserNotificationCenterDelegate()
NotificationSettings.delegate = '<Your Current Center Delegate>'
return true
}
然后,创建一个函数来处理 APNS/FCM JSON 结果
fileprivate var info: NotificationInfo?
func buildNotification(data: [AnyHashable: Any]) {
// First, save data to model NotificationInfo
setNotificationInfo(data)
if let info = info {
// Second, create NotificationBuilderHelper instance by receive data and your custom notification id
let buildHelper = NotificationBuilderHelper(withMessage: data, notificationId: info.getNotificationId())
// Third, connect to extension NotificationBuilderDelegate
buildHelper.delegate = self
// Fourth, create notification with optional actions and show the banner
let actionsContext: [ActionContext] = [("Action1Title", .foreground)]
buildHelper.createNotification(with: .defaultSound, add: actionsContext)?.show()
}else {
print("Never initialize model of notificationInfo")
}
}
NotificationBuilderDelegate
在使用 NotificationBuilderHelper 类时必须继承 NotificationBuilderDelegate
public protocol NotificationBuilderDelegate: class {
func getNotificationInfo() -> NotificationInfo
func getNotificationIdentityDic() -> Dictionary<String, NotificationIdentity>?
}
可选 API
NotificationBuilderHelper
-
updateMsgData
在调用 createNotification 或 createNotificationByCustom 之前,通过您的新字典添加或更新通知内容 userinfo
-
createNotificationByCustom
您可以调用 createNotificationByCustom 并利用自定义的 getNotificationIdentityDic 来定义所有必要的通知标识
作者
minyingwu, [email protected]
许可
SwiftNotification 在 MIT 许可下可用。更多信息请参阅 LICENSE 文件。