我正在使用SwiftUI。
我希望在点击推送通知时打开除根视图之外的特定屏幕。有几种使用StoryBoard打开它的方法,但没有不使用StoryBoard的。
如何在没有使用StoryBoard的情况下实现呢?
我尝试过这个,但我是初学者,所以不知道怎么做。
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()
}
... }
userNotificationCenter(_ center: UNUserNotificationCenter, didReceive...)
在应用程序处于后台时被调用,所以此时没有UI存在,你只需要将通知响应数据存储在某个地方,以便稍后向用户显示,当用户打开应用程序时,应用程序就像正常流程一样运行(例如从数据库中读取记录、默认值等)。 - Asperi