0

I found some posts already on stack overflow but they are old and not actual anymore.

I have a problem with implementing Push Notifications.

Xcode 11, iOS 13, App just created so the certificates are for Xcode 11 or later, Push Notifications & Backgroundmodes (processing, remote notifications, fetch) are enabled.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        //Firebase Messaging

        registerForPushNotifications()

        UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
        Messaging.messaging().delegate = self as MessagingDelegate

//        //Locationtracking
//
//        guard let isLocationEnabled = UserDefaults.standard.value(forKey: "userEnabledLocationTracking") as? Bool else {locationManager.stopLocationTracking(); return true}
//
//        isLocationEnabled ? locationManager.startLocationTracking() : locationManager.stopLocationTracking()

        return true
    }

After that I am calling this 2 Functions:

func registerForPushNotifications() {
        UNUserNotificationCenter.current()
            .requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, error in
                print("Permission granted: \(granted)")
                guard granted else {return}
                self?.getNotificationSettings()
        }
    }

    func getNotificationSettings() {
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            print("Notification settings: \(settings)")
            guard settings.authorizationStatus == .authorized else {return}
                DispatchQueue.main.async {
                    print("register called.......")
                    UIApplication.shared.registerForRemoteNotifications()
                }
        }
    }

But my "didRegisterForRemoteNotificationsWithDeviceToken" - Delegate method is not being called and I cannot understand why this happens.

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Auth.auth().setAPNSToken(deviceToken, type: .unknown)
        Messaging.messaging().apnsToken = deviceToken

        let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
        let token = tokenParts.joined()
        print("Device Token: \(token)")
    }

Would be great if anyone can help me and tell me where's my mistake at.

Have a nice day!

alarsut
  • 41
  • 4
  • Does this answer your question? [didRegisterForRemoteNotificationsWithDeviceToken not called in ios8, but didRegister...Settings is](https://stackoverflow.com/questions/28128490/didregisterforremotenotificationswithdevicetoken-not-called-in-ios8-but-didregi) – Mat Mar 21 '20 at 17:53
  • I guess you are not using the simulator to test it right? – Mat Mar 21 '20 at 17:58
  • I did not change anything.. Just waited 4 hours and then run the code again on my iPhone and now it worked.... really don't know why... – alarsut Mar 22 '20 at 00:35

0 Answers0