gpt4 book ai didi

ios - 如何为 didReceiveRemoteNotification 生成组合发布者?

转载 作者:行者123 更新时间:2023-12-04 08:42:07 24 4
gpt4 key购买 nike

我正在尝试从 didReceiveRemoteNotification 生成一个联合发布者
类似于下面的这段代码:

NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
我想使用 SwiftUI Lifecycle 并且不想使用 AppDelegate 方法使用 @UIApplicationDelegateAdaptor

最佳答案

没有Notification命名 didReceiveRemoteNotification ,但您可以在 UIApplication 的扩展中声明它:

extension UIApplication {
static let didReceiveRemoteNotification = Notification.Name("didReceiveRemoteNotification")
}
那么你需要发布 Notification来自 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)

关于ios - 如何为 didReceiveRemoteNotification 生成组合发布者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64512868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com