如何为 didReceiveRemoteNotification 生成 Combine Publisher?
How to generate a Combine Publisher for didReceiveRemoteNotification?
我正尝试在 didReceiveRemoteNotification
下生成一个 Combine 发布者
类似于下面的代码:
NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
我想使用 SwiftUI Lifecycle 而不想使用 AppDelegate 方法 @UIApplicationDelegateAdaptor
没有名为 didReceiveRemoteNotification
的 Notification
,但您可以在 UIApplication
上的扩展中声明它:
extension UIApplication {
static let didReceiveRemoteNotification = Notification.Name("didReceiveRemoteNotification")
}
然后你需要 post 来自 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...
}
}
这将允许您使用通常的语法:
NotificationCenter.default.publisher(for: UIApplication.didReceiveRemoteNotification)
我正尝试在 didReceiveRemoteNotification
类似于下面的代码:
NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
我想使用 SwiftUI Lifecycle 而不想使用 AppDelegate 方法 @UIApplicationDelegateAdaptor
没有名为 didReceiveRemoteNotification
的 Notification
,但您可以在 UIApplication
上的扩展中声明它:
extension UIApplication {
static let didReceiveRemoteNotification = Notification.Name("didReceiveRemoteNotification")
}
然后你需要 post 来自 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...
}
}
这将允许您使用通常的语法:
NotificationCenter.default.publisher(for: UIApplication.didReceiveRemoteNotification)