Ios 如何拦截来自共享扩展的远程通知?
在iOS应用程序中,要拦截/读取/获取远程通知,我们必须实施UIApplicationLegate方法:Ios 如何拦截来自共享扩展的远程通知?,ios,objective-c,swift,apple-push-notifications,Ios,Objective C,Swift,Apple Push Notifications,在iOS应用程序中,要拦截/读取/获取远程通知,我们必须实施UIApplicationLegate方法: func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {}
但不可能以相同的方式从共享扩展中截获/读取/获取远程通知
如何像在iOS应用程序中那样拦截来自共享扩展的远程通知?我找到了解决方案如何这样做: a。共享扩展和通知服务扩展应设置为同一应用程序组。可以在此处找到如何执行此操作的参考资料:& b。在Apple推送通知中添加“mutable content=true”
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard context == ¬ificationServices else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
// Update depending on notification.
}
c。实现通知服务扩展
d。在didReceive(\uRequest:UNNotificationRequest,withContentHandler:contentHandler:@escaping(UNNotificationContent)->Void)函数中,使用通知键将通知用户信息保存到用户默认值
e。在共享扩展中,为**notificationKey**为UserDefault添加KVO
private var notificationServices = 1
let group = UserDefaults(suiteName: SGConst.groupIdentifier)
group.addObserver(self, forKeyPath: "notificationKey", options: .new, context: ¬ificationServices)
f。更新**notificationKey**的值时,根据通知获取用户信息和更新视图
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard context == ¬ificationServices else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
// Update depending on notification.
}
你能说清楚你想要什么吗?@zombie添加了更多细节。你不能。只有当有人在“共享”对话框中选择共享扩展时,才会启动该扩展。您的主机应用程序可以接收通知并存储一些信息,以便下次启动时与共享扩展共享。@Paulw11我找到了解决方法。我已经搜索了很长时间了。这样,即使用户没有单击通知,您也可以保存通知的内容,对吗?如果是这样的话,你可以分享更多关于步骤1-5-6的细节吗?我们如何做到这一点尚不清楚。感谢you@MBH你是对的。即使没有用户交互,它也会被保存。我将能够添加更多的信息只在几天内。超级棒!你能在github上分享一些细节或示例项目吗?@MBH我发布了更多细节和如何实现它的URL。是否有足够的信息?仍然无法实施,当我收到通知时,什么都没有发生