SwiftUI App Protocol and Remote Notifications

I am trying to bring CloudKit syncing to a new SwiftUI app using the new App protocol and have not found a way to listen for remote notifications. Is this possible with the new updates to SwiftUI this year or is this currently an AppDelegate thing only?
Answered by Frameworks Engineer in 615300022
Hi CracchioloNick,

If your app needs to use traditional App Delegate methods for these types of lifecycle events, you can use the NSApplicationDelegateAdaptor or UIApplicationDelegateAdaptor property wrappers to supply a type that should handle those delegate methods.

You can use it like this:

Code Block swift
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
var body: some Scene {
WindowGroup {
RootView()
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
}


With this declaration, a single instance of your App Delegate class will be created to handle any of the delegate methods that it implements, including push notification callbacks and/or CloudKit sharing invitations.
Accepted Answer
Hi CracchioloNick,

If your app needs to use traditional App Delegate methods for these types of lifecycle events, you can use the NSApplicationDelegateAdaptor or UIApplicationDelegateAdaptor property wrappers to supply a type that should handle those delegate methods.

You can use it like this:

Code Block swift
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
var body: some Scene {
WindowGroup {
RootView()
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
}


With this declaration, a single instance of your App Delegate class will be created to handle any of the delegate methods that it implements, including push notification callbacks and/or CloudKit sharing invitations.
Awesome! I didn't see these. Thank you for your help!
I saw the reply from the Frameworks Engineer, however it doesn't appear that all delegate callbacks proper functionally. There's a related question with not much help - https://developer.apple.com/forums/thread/669962

The provided solutions absolutely do NOT work. The only delegate method that gets called this way is didFinishLaunchingWithOptions. That's fine and all, but what if you need to register for push notifications?? Any real app needs the token to send to a server so it can send out pushes to the user.

Seriously, this is very messed up.

Neither didRegisterForRemoteNotificationsWithDeviceToken, nor didFailToRegisterForRemoteNotificationsWithError gets called. There is a message in logs "Registered for push notifications with token: <...>", which probably means registration itself succeeds. Push notifications capability is added, certificates are in place, I am testing on device, not sim, but AppDelegate's methods for pushes are just not called. didFinishLaunchingWithOptions is called normally btw, which means my UIApplicationDelegateAdaptor is ok. I am using xcode Version 13.2.1 and ios 15.2.

Here is how I subscribe to pushes:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
      NSLog("PUSH NOTIFICATION PERMISSION GRANTED: \(granted)")
      guard granted else { return }
      DispatchQueue.main.async {
        UIApplication.shared.registerForRemoteNotifications()
      }
    }

Here are method signatures:

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { }
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { }

I need to receive device token to send to server, please help

SwiftUI App Protocol and Remote Notifications
 
 
Q