SwiftUI通知点击进入特定视图。

5

我正在使用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

        }
    

    }

}
2个回答

8

我曾经面对过完全相同的问题,我这样解决:

我让我的AppDelegate类符合ObservableObject协议,并添加了一个发布的属性,用于控制是否需要显示特定于通知的视图:@Published var openedFromNotification: Bool = false

AppDelegate中,我将此属性设置为true,位于userNotificationCenter(... willPresent...)(当应用程序处于活动状态时)或userNotificationCenter(... didReceive ...)(当应用程序处于后台时)函数内。

由于 AppDelegate 是一个 ObservableObject,它可以被设置为 ContentViewenvironmentObject

@main
struct swiftUiApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

var body: some Scene {
     
     WindowGroup {
        
        ContentView().environmentObject(delegate)
     
     }
  } 
}

这使我能够在 ContentView() 中访问已发布的属性。

在 ContentView() 中,根据 delegate.openedFromNotification 属性,我只需显示特定于通知的视图或正常打开应用程序时的标准视图:

struct ContentView: View {
    
    @EnvironmentObject private var delegate: AppDelegate
    
    var body: some View {

       if (delegate.openedFromNotification) {
          SpecialView().environmentObject(delegate)
       } else {
           HomeView()
       }

    }

}

我将EnvironmentObject delegate 传递给SpecialView(),以便在需要再次显示标准HomeView()时可以将openedFromNotification属性设置为false。

如果您想要显示不同的视图,例如某些通知负载,可以通过添加更多的published属性来扩展此功能。

在我的情况下,我有两个这样的属性,并且根据推送通知数据中的JSON值,我可以将用户导航到应用程序的不同部分。


这也可以在watchOS扩展中完成,只需添加WKExtensionDelegate而不是AppDelegate,并在其中添加UNUserNotificationCenter.current().delegate = selfapplicationDidFinishLaunching中。 - Viktor Sec
谢谢。我在 https://github.com/binwiederhier/ntfy-ios/commit/30dd10bb80beacbefaefe8f6dfb87a104893c21e 中实现了这个功能,现在它完美运行。 - binwiederhier
在经历了很多困难之后,我找到了这个宝石。对我有用!谢谢 - cherucole
额外信息:由于AppDelegate的已发布属性是@State对象,因此您可以将它们用作绑定,例如表单修饰符 - 这样,您可以将特定于通知的视图显示为弹出窗口,而不是全屏视图。只需在弹出表单的视图的onDismiss修饰符中将属性设置回false,以完成循环,以便下一个推送通知可以再次触发表单。 - kk94

0

这是你要找的吗?

@main
struct swiftUiApp: App {

    
    @AppStorage("ViewSelection") var ViewSelection = "SetupView" // Can be another type

    var body: some Scene {

        WindowGroup {
            TabView(selection: $ViewSelection) {

                SetupView().tag("SetupView")
                ContentView().tag("ContentView")

            }
        .tabViewStyle(PageTabViewStyle())
        .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))

        }

    }

}

只需确保将视图名称更改为您自己的

ViewSelection是一个绑定,所以只需要像这样

ViewSelection = "ContentView"

会改变它


我需要从应用程序委托中更改视图,因为我需要传递参数,否则这将无法工作。 - anonS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接