swiftswiftuinotificationsappdelegate

Unable to pass information from local notification in AppDelegate to a view in SwiftUI


I have a scheduled notification that works and I am attempting to get information from that notification to open another view, but right now I'm unable to get that information passed into my main view in my onReceive portion of the code.

I can see the information in my AppDelegate portion of the code.

I feel like there's a small step I'm missing somewhere.

tl;dr: trying to pass notification information from AppDelegate to MainView

@main
struct UntitledNewsApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    @StateObject var notificationData = NotificationData()
    
    
    var body: some Scene {
        WindowGroup {
            MainView()
                .environmentObject(notificationData)
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    var notificationData = NotificationData()
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        UNUserNotificationCenter.current().delegate = self
        return true
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // Handle the received notification and navigate to the desired view
        if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
            let articleResponse = response.notification.request.content
            
            notificationData.notificationTitle = articleResponse.title
            
            DispatchQueue.main.async {
                if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
                    appDelegate.notificationData.notificationTitle = articleResponse.title
                }
            }
            // Notification tapped, navigate to the desired view
            NotificationCenter.default.post(name: NSNotification.Name("NotificationTapped"), object: nil)
        }
        
        completionHandler()
    }
}

class NotificationData: ObservableObject {
    @Published var notificationTitle: String = ""
}

struct MainView: View {
  @EnvironmentObject var notificationData: NotificationData
    ...
   var body: some View {
Text("Never Give You Up")
        .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name("NotificationTapped"))) { notification in
            print("onReceive in blank: \(notificationData.notificationTitle)")
        }
    }
}

Solution

  • Every time you call NotificationData() you create a different instance, one does not know about the other.

    You can change your injection to connect.

    .environmentObject(appDelegate.notificationData)
    

    You don't need the @StateObject the AppDelegate will manage lifecycle.