【问题标题】:SwiftUI notification click go to a specific viewSwiftUI 通知单击转到特定视图
【发布时间】:2021-01-29 20:59:57
【问题描述】:

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

        }
    

    }

}

【问题讨论】:

    标签: ios push-notification swiftui ios14 firebase-notifications


    【解决方案1】:

    这就是你要找的吗?

    @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"
    

    会改的

    【讨论】:

    • 我需要从应用代理更改视图,因为我需要传递参数,这不起作用
    【解决方案2】:

    我遇到了完全相同的问题并以这种方式解决了它:

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

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

    由于AppDelegate 是一个ObservableObject,它可以设置为一个environmentObject 用于ContentView:

    @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。

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

    在我的例子中,我有两个这样的已发布属性,并且基于推送通知数据中的 JSON 值,我可以将用户导航到我的应用程序的不同部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多