如何拦截来自共享扩展的远程通知?

时间:2017-03-20 10:38:33

标签: ios objective-c swift apple-push-notifications

在iOS应用程序中拦截/读取/获取远程通知我们必须实施 UIApplicationDelegate 方法:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {}

但是无法通过共享扩展以相同的方式拦截/读取/获取远程通知。

如何在iOS应用中拦截来自共享扩展的远程通知?

1 个答案:

答案 0 :(得分:1)

我找到了解决方法如何以这种方式做到:

<强>一个即可。共享扩展和Notification Services扩展应设置为相同的应用程序组。有关如何操作的参考资料,请访问:Apple: Configuring App Groups&amp; second

<强> B'/ strong>即可。添加&#34; mutable-content = true &#34;到Apple推送通知。

<强> C 即可。实施Notification Services Extension。

<强> d 即可。在 didReceive(_ request:UNNotificationRequest,withContentHandler contentHandler:@escaping(UNNotificationContent) - &gt; Void) func使用 notificationKey 将userInfo保存通知到UserDefaults。

<强>电子即可。在共享扩展中为UserDefaults添加** notificationKey **的KVO。

private var notificationServices = 1
let group = UserDefaults(suiteName: SGConst.groupIdentifier)
group.addObserver(self, forKeyPath: "notificationKey", options: .new, context: &notificationServices)

<强>˚F即可。当** notificationKey **的值更新时,根据通知获取userInfo和update视图。

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    guard context == &notificationServices else {
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        return
    }
    // Update depending on notification.
}
相关问题