我正在使用SwiftUI 2.0并尝试实现Firebase推送通知。在新的SwiftUI应用程序结构中,没有AppDelegate和SceneDelegate,因此我创建了AppDelegate类。我成功收到了通知,但是无法在通知点击时跳转到特定视图。
以下是我的代码:
@main
struct swiftUiApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
AppDelegate扩展:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let orders = Orders()
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
if(userInfo["gcm.notification.type"] != nil){
if(userInfo["gcm.notification.type"] as! String == "order"){
//here I need to navigate to OrderView
}
}
}
UNUserNotificationCenter.current().delegate = self
到applicationDidFinishLaunching
中。 - Viktor SecAppDelegate
的已发布属性是@State
对象,因此您可以将它们用作绑定,例如表单修饰符 - 这样,您可以将特定于通知的视图显示为弹出窗口,而不是全屏视图。只需在弹出表单的视图的onDismiss
修饰符中将属性设置回false,以完成循环,以便下一个推送通知可以再次触发表单。 - kk94