本文介绍了如何为didReceiveRemoteNotification生成Combine Publisher?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据 didReceiveRemoteNotification
类似于下面的代码:
NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
我想使用SwiftUI生命周期,并且不想通过 @UIApplicationDelegateAdaptor
I want to use SwiftUI Lifecycle and don't want to use AppDelegate methods using @UIApplicationDelegateAdaptor
推荐答案
没有名为 didReceiveRemoteNotification
的 Notification
,但是您可以在的扩展名中对其进行声明UIApplication
:
There is no Notification
named didReceiveRemoteNotification
, but you can declare it in an extension on UIApplication
:
extension UIApplication {
static let didReceiveRemoteNotification = Notification.Name("didReceiveRemoteNotification")
}
然后,您需要从 AppDelegate
发布 Notification
:
extension AppDelegate {
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NotificationCenter.default.post(name: UIApplication.didReceiveRemoteNotification,
object: nil)
// etc...
}
}
这将允许您使用常规语法:
That will allow you to use the usual syntax:
NotificationCenter.default.publisher(for: UIApplication.didReceiveRemoteNotification)
这篇关于如何为didReceiveRemoteNotification生成Combine Publisher?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!