When using the SwiftUI declarations to define an app, where do you put the code for registering for notifications that would normally go in the app delegate?
How does notification registration work in the SwiftUI app lifecycle?
Here is a sample code of how your App struct should look like:
Code Block swift @main struct NewIn14App: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } } }
Taken from hackingwithswift.com
I put it inside applicationDidFinishLaunching as follows:
Code Block notificationCenter = UNUserNotificationCenter.current() notificationCenter.removeAllPendingNotificationRequests() let options: UNAuthorizationOptions = [.alert, .sound, .badge] notificationCenter.requestAuthorization(options: options) { (didAllow, error) in if !didAllow { print("User has declined notifications") } }