[ad_1]
I’ve an AppDelegate in my SwiftUI app, and I set a ViewModel like so:
class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
var AppNavigator: ApplicationNavigator = ApplicationNavigator()
}
That is my ApplicationNavigator View Mannequin:
class ApplicationNavigator: ObservableObject {
@Revealed var applicationNavigation: ApplicationNavigatorItem = ApplicationNavigatorItem(id: "default")
}
The place ApplicationNavigator takes an ApplicationNavigatorItem:
struct ApplicationNavigatorItem: Identifiable, Codable {
var id: String? = UUID().uuidString
}
And when a person faucets on a distant notification, I replace the AppNavigator in my AppDelegate with a brand new ApplicationNavigatorItem:
func userNotificationCenter(_ middle: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
self.AppNavigator.applicationNavigation = ApplicationNavigatorItem(
id: newUUID
)
}
Now to the SwiftUI half.
In my views, I’ve shared appDelegate as an setting object with my view, since I wish to hear for adjustments to appDelgate.AppNavigator.applicationNavigation.id.
I’ve set an onChanged modifier to my major view:
.onChange(of: appDelegate.AppNavigator.applicationNavigation.id) { _ in
DispatchQueue.major.async {
appDelegate.AppNavigator.navigate()
}
}
And this solely works if the person is tapping the push notification when the app is within the background.
Nonetheless, if a person receives the push notification whereas they’re contained in the app, and it’s energetic, nothing occurs after they faucet it.
Oddly sufficient, after tapping a notification whereas contained in the app, (the place nothing occurs) if I swipe the app up till it is scenePhase is .inactive || .background, and re-open the app, the code inside .onChange(..) { _ in // code } lastly runs.
I’ve even put a Textual content(appDelegate.AppNavigator.applicationNavigation.id!) on the principle view to see if appDelegate.AppNavigator.applicationNavigation.id has modified, but it surely does not if I am nonetheless contained in the app.
I am attempting to ship the person to a particular view in the event that they faucet on a notification, and typically they is likely to be contained in the app. Can anybody assist me see the place I’ve made a mistake?
[ad_2]
