UILocalNotification-RemotePayload 2.0.1

UILocalNotification-RemotePayload 2.0.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2016年9月
SPM支持 SPM

Thanh Pham 维护。



  • Thanh Pham
'   _     _  _     ____  ____  ____  _     _      ____  _____  _  _____ _  ____  ____  _____  _  ____  _     
'  / \ /\/ \/ \   /  _ \/   _\/  _ \/ \   / \  /|/  _ \/__ __\/ \/    // \/   _\/  _ \/__ __\/ \/  _ \/ \  /|
'  | | ||| || |   | / \||  /  | / \|| |   | |\ ||| / \|  / \  | ||  __\| ||  /  | / \|  / \  | || / \|| |\ ||
'  | \_/|| || |_/\| \_/||  \_ | |-||| |_/\| | \||| \_/|  | |  | || |   | ||  \_ | |-||  | |  | || \_/|| | \||
'  \____/\_/\____/\____/\____/\_/ \|\____/\_/  \|\____/  \_/  \_/\_/   \_/\____/\_/ \|  \_/  \_/\____/\_/  \|
'                                                                                                            
'   ____  _____ _      ____  _____  _____ ____  ____ ___  _ _     ____  ____  ____                           
'  /  __\/  __// \__/|/  _ \/__ __\/  __//  __\/  _ \\  \/// \   /  _ \/  _ \/  _ \                          
'  |  \/||  \  | |\/||| / \|  / \  |  \  |  \/|| / \| \  / | |   | / \|| / \|| | \|                          
'  |    /|  /_ | |  ||| \_/|  | |  |  /_ |  __/| |-|| / /  | |_/\| \_/|| |-||| |_/|                          
'  \_/\_\\____\\_/  \|\____/  \_/  \____\\_/   \_/ \|/_/   \____/\____/\_/ \|\____/                          
'                                                                                                            

UILocalNotification-RemotePayload

描述

此扩展提供了一种方便的初始化方法,可以从在Apple 的文档中指定的远程加载项创建 UILocalNotification 实例

这与我们对自己的本地通知有更多控制权的事实有关。使用本地通知,我们可以拥有类似 Gmail 应用(Gmail)或 Facebook 消息应用(Messenger)中看到的行为的通知。当您在浏览器上阅读电子邮件时,Gmail 应用会自动清除您的 iOS 设备上已读电子邮件的通知。消息应用的通知会在您的屏幕上多次振动并播放声音,而不会填充多个通知。Google 和 Facebook 可能不会使用此处讨论的相同技术,但我们可以使用下面的技术为我们的应用程序创建具有这些高级行为的通知。

想法是,我们不是推送远程通知并让 iOS 将其显示给用户,而是推送一个静默通知,这将唤醒我们的应用程序在后台运行,然后后台代码从加载项创建一个本地通知并将其显示给用户。由于我们持有本地通知实例,我们可以在任何时候将其从屏幕上删除。此技术仅在用户设备上启用了后台应用刷新并且您的应用已注册后台执行时才有效。但是,如果您的应用程序是一个使用 PushKit 的 VoIP 应用程序,则即使在后台应用刷新已禁用的情况下,该技术也有效。

设置

  • 在您的应用程序的 Info.plist 文件中启用后台模式

非 VoIP 应用程序

<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

VoIP 应用程序

<key>UIBackgroundModes</key>
<array>
    <string>voip</string>
</array>

  • 注册本地通知权限
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil))
  • 注册远程通知

非 VoIP 应用程序

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
}

VoIP 应用程序

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 添加更新通知的逻辑。

非 VoIP 应用程序

推送一个静默的远程通知。将加载项放在除顶级 aps 之外的对象中,以便通知保持静默。这里我们把它放在了 payload 对象中。

{
    "aps" : {
        "content-available": 1
    },
    "payload" : {
        "aps" : {
            "alert" : "hello world"
        },
        "id" : 1
    }
}

VoIP 应用程序

只推送一个普通加载项,因为 VoIP 通知始终是静默的。

{
    "aps" : {
        "alert" : "hello world"
    },
    "id" : 1
}

  • 处理远程通知

非 VoIP 应用程序

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)
}

VoIP 应用程序

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)
}

用法

Swift

let localNotification = UILocalNotification(remotePayload: remotePayload)

Objective-C

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 文件。