gpt4 book ai didi

swift - 我想点击推送通知以在SwiftUI中打开特定屏幕

转载 作者:行者123 更新时间:2023-12-03 09:22:05 27 4
gpt4 key购买 nike

我正在使用SwiftUI。

我想通过单击推送通知来打开除“根 View ”以外的特定屏幕。有多种使用StoryBoard打开它的方法,但并非没有StoryBoard才能打开它。

不使用StoryBoard怎么办?

我尝试了this,但是我是初学者,所以我不知道。

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?

func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)
-> Void) {
completionHandler([.alert, .badge, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
// I want to open a screen other than Root View here.
completionHandler()
}
... }

最佳答案

想法是,当用户来自通知时,您设置一个变量,并在您要显示UI时检查该变量。
这是一个示例:

// assume that AppDelegate is also our UNNotificationCenterDelegate 
// I'm using a bool variable to check if user is coming from the notification
var isFromNotif: Bool = false
extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

isFromNotif = true
// ...

}
现在在我的 View中,我检查标志。
struct ContentView1: View {

var body: some View {
return Group {
if isFromNotif {
Text("coming from notification")
} else {
Text("not coming from notification")
}
}
}

}
希望本样本对您有所帮助。

关于swift - 我想点击推送通知以在SwiftUI中打开特定屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61221154/

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