function didRegisterForRemoteNotificationsWithDeviceToken not running (Xcode 13)

Viewed 88

I am trying to print out the device token but I can't even get the didRegisterForRemoteNotificationsWithDeviceToken function to run. i have looked at all the online tutorials and nothing. i am using firebase auth and firestore if that means something. Here is my code.

import SwiftUI
import Firebase

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        
        return true
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("deviceToken")
        let token = deviceToken
            .map { String(format: "%02.2hhx", $0) }
            .joined()
        
        print(token)
    }
}

@main
struct PrayerUmmahApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            NavigationView {
                ContentView()
                    .onAppear{
                        let center = UNUserNotificationCenter.current()
                        center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
                            
                            if let error = error {
                                print(error)
                            }
                            print("Granted \(granted)")
                            DispatchQueue.main.async {
                                UIApplication.shared.registerForRemoteNotifications()
                            }
                        }
                    }
            }
        }
    }
}

0 Answers
Related