Reputation: 53
I am using SwiftUI 2.0 and I am trying to implement firebase push notifications. In new SwiftUI app structure, there's no AppDelegate and SceneDelegate, so I created AppDelegate class. I managed receiving notifications but I cant go to a specific view on notification click. Here is my code:
@main
struct swiftUiApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
And AppDelegate extension:
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
}
}
}
Upvotes: 5
Views: 3421
Reputation: 203
I was facing the exact same problem and solved it this way:
I have my AppDelegate
class conform to ObservableObject
and added a published property that controls whether I need to show the notification-specific view: @Published var openedFromNotification: Bool = false
In the AppDelegate
, I set this property to true
inside the userNotificationCenter( ... willPresent ...)
(notification when app is active) or userNotificationCenter( ... didReceive ...)
(notification when app is in background) functions.
Since AppDelegate
is an ObservableObject
, it can be set as an environmentObject
for ContentView:
@main
struct swiftUiApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView().environmentObject(delegate)
}
}
}
This gives me access to the published property inside my ContentView().
Inside ContentView I simply display the notification-specific view or the standard view for when the app is opened normally based on the delegate.openedFromNotification
property:
struct ContentView: View {
@EnvironmentObject private var delegate: AppDelegate
var body: some View {
if (delegate.openedFromNotification) {
SpecialView().environmentObject(delegate)
} else {
HomeView()
}
}
}
I pass the EnvironmentObject delegate
on to the SpecialView() so that I can set the openedFromNotification
property back to false once I need to show the standard HomeView() again.
This can be expanded by adding more published properties if you want to have different Views to be shown for e.g. certain notification payloads.
In my case I have two such published properties, and based on a JSON value in the push notification data I can navigate the user to different parts of my app.
Upvotes: 8
Reputation: 3
Is this what you're looking for?
@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))
}
}
}
Just make sure to change the view names to your own
The ViewSelection is a binding so just something like
ViewSelection = "ContentView"
will change it
Upvotes: 0