I have a silent remote notification coming in on a SwiftUI app. It is not picked up by the UNUserNotificationCenter, but rather, by the old AppDelegate didReceiveNotification func. What is the solution for notifying ContentView() that a change has occurred? An ObservableObject in AppDelegate?
1 Answer
You can declare a store which conforms ObservableObject in the AppDelegate and set it as an environment object to the ContentView.
// AppDelegate.swift
// in the class
let store = Store()
// in the applicationDidFinishLaunching(_:) method
window.contentView = NSHostingView(rootView: contentView.environmentObject(store))
// ContentView.swift
// in the struct
@EnvironmentObject var store: Store
The environment object is a reference so you can pass the value into it. Same to using ObservableObject. When you finish updating just call objectWillChange.send() or mark the property to @Published. And ContentView will be updated after notified.