Ios 如何为DidReceiveMotonification生成联合发布者?
我正在尝试通过didReceiveMemotentification生成一个联合发布者 与下面的代码类似:Ios 如何为DidReceiveMotonification生成联合发布者?,ios,swift,swiftui,combine,Ios,Swift,Swiftui,Combine,我正在尝试通过didReceiveMemotentification生成一个联合发布者 与下面的代码类似: NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification) 我想使用SwiftUI Lifecycle,不想使用AppDelegate方法使用@UIApplicationDelegateAdaptor没有名为DidReceiveMotonification的通知,但您可以
NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
我想使用SwiftUI Lifecycle,不想使用AppDelegate方法使用
@UIApplicationDelegateAdaptor
没有名为DidReceiveMotonification
的通知,但您可以在UIApplication
的扩展中声明它:
extension UIApplication {
static let didReceiveRemoteNotification = Notification.Name("didReceiveRemoteNotification")
}
然后您需要从AppDelegate
发布通知
:
extension AppDelegate {
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NotificationCenter.default.post(name: UIApplication.didReceiveRemoteNotification,
object: nil)
// etc...
}
}
这将允许您使用常用语法:
NotificationCenter.default.publisher(for: UIApplication.didReceiveRemoteNotification)
No.NotificationCenter.default.publisher(用于:UIApplication.DidReceiveMemoteNotification)抛出错误。=>类型“UIApplication”没有成员“DidReceiveMemoteNotification”。在收到远程通知时没有发布通知。您需要实现app delegate方法,并从那里发送到发布服务器。确定。谢谢你的回答,非常感谢。