测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2016年9月 |
SPM支持 SPM | ✓ |
由Thanh Pham 维护。
' _ _ _ ____ ____ ____ _ _ ____ _____ _ _____ _ ____ ____ _____ _ ____ _
' / \ /\/ \/ \ / _ \/ _\/ _ \/ \ / \ /|/ _ \/__ __\/ \/ // \/ _\/ _ \/__ __\/ \/ _ \/ \ /|
' | | ||| || | | / \|| / | / \|| | | |\ ||| / \| / \ | || __\| || / | / \| / \ | || / \|| |\ ||
' | \_/|| || |_/\| \_/|| \_ | |-||| |_/\| | \||| \_/| | | | || | | || \_ | |-|| | | | || \_/|| | \||
' \____/\_/\____/\____/\____/\_/ \|\____/\_/ \|\____/ \_/ \_/\_/ \_/\____/\_/ \| \_/ \_/\____/\_/ \|
'
' ____ _____ _ ____ _____ _____ ____ ____ ___ _ _ ____ ____ ____
' / __\/ __// \__/|/ _ \/__ __\/ __// __\/ _ \\ \/// \ / _ \/ _ \/ _ \
' | \/|| \ | |\/||| / \| / \ | \ | \/|| / \| \ / | | | / \|| / \|| | \|
' | /| /_ | | ||| \_/| | | | /_ | __/| |-|| / / | |_/\| \_/|| |-||| |_/|
' \_/\_\\____\\_/ \|\____/ \_/ \____\\_/ \_/ \|/_/ \____/\____/\_/ \|\____/
'
此扩展提供了一种方便的初始化方法,可以从在Apple 的文档中指定的远程加载项创建 UILocalNotification 实例。
这与我们对自己的本地通知有更多控制权的事实有关。使用本地通知,我们可以拥有类似 Gmail 应用(Gmail)或 Facebook 消息应用(Messenger)中看到的行为的通知。当您在浏览器上阅读电子邮件时,Gmail 应用会自动清除您的 iOS 设备上已读电子邮件的通知。消息应用的通知会在您的屏幕上多次振动并播放声音,而不会填充多个通知。Google 和 Facebook 可能不会使用此处讨论的相同技术,但我们可以使用下面的技术为我们的应用程序创建具有这些高级行为的通知。
想法是,我们不是推送远程通知并让 iOS 将其显示给用户,而是推送一个静默通知,这将唤醒我们的应用程序在后台运行,然后后台代码从加载项创建一个本地通知并将其显示给用户。由于我们持有本地通知实例,我们可以在任何时候将其从屏幕上删除。此技术仅在用户设备上启用了后台应用刷新并且您的应用已注册后台执行时才有效。但是,如果您的应用程序是一个使用 PushKit
的 VoIP 应用程序,则即使在后台应用刷新已禁用的情况下,该技术也有效。
Info.plist
文件中启用后台模式<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
<key>UIBackgroundModes</key>
<array>
<string>voip</string>
</array>
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil))
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// pass the deviceToken to the server
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .None {
let pushRegistry = PKPushRegistry(queue: dispatch_get_main_queue())
pushRegistry.delegate = self
pushRegistry.desiredPushTypes = [PKPushTypeVoIP]
}
}
extension AppDelegate: PKPushRegistryDelegate {
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
// pass credentials.token to the server
}
}
id
,这样您就可以根据其 id
添加更新通知的逻辑。推送一个静默的远程通知。将加载项放在除顶级 aps
之外的对象中,以便通知保持静默。这里我们把它放在了 payload
对象中。
{
"aps" : {
"content-available": 1
},
"payload" : {
"aps" : {
"alert" : "hello world"
},
"id" : 1
}
}
只推送一个普通加载项,因为 VoIP 通知始终是静默的。
{
"aps" : {
"alert" : "hello world"
},
"id" : 1
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
let localNotification = UILocalNotification(remotePayload: userInfo["payload"] as? [NSObject: AnyObject] ?? [:])
if let id = userInfo["payload"]?["id"] as? Int {
if let previousLocalNotification = self.localNotificationsMap[id] {
UIApplication.sharedApplication().cancelLocalNotification(previousLocalNotification)
}
self.localNotificationsMap[id] = localNotification
}
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
completionHandler(.NoData)
}
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
let localNotification = UILocalNotification(remotePayload: payload.dictionaryPayload)
if let id = payload.dictionaryPayload["id"] as? Int {
if let previousLocalNotification = self.localNotificationsMap[id] {
UIApplication.sharedApplication().cancelLocalNotification(previousLocalNotification)
}
self.localNotificationsMap[id] = localNotification
}
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
let localNotification = UILocalNotification(remotePayload: remotePayload)
UILocalNotification *localNotification = [[UILocalNotification alloc] initWithRemotePayload:remotePayload];
将文件 /UILocalNotification-RemotePayload/UILocalNotification-RemotePayload.swift
添加到你的项目中。你已经设置完毕。
从版本 2.0.0 开始,使用 Swift 3 语法。如果你的项目仍然使用 Swift 版本 2,请使用 2.0.0 以前的 UILocalNotification-RemotePayload 版本。
Podfile
pod 'UILocalNotification-RemotePayload', '~> 1.0.4'
或 Cartfile
github "T-Pham/UILocalNotification-RemotePayload" ~> 1.0.4
UILocalNotification-RemotePayload 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。