'didRegisterForRemoteNotificationsWithDeviceToken' never called

Hello everyone, I need some help.

I have absolutely no clue why my notifications are not working.

My AppDelegate looks like this:

Code Block
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    /// The base UIWindow
    var window: UIWindow?
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("token: \(token)")
    }
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to register for remote notifications with error: \(error))")
    }
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        UNUserNotificationCenter.current().delegate = self
        UIApplication.shared.registerForRemoteNotifications()
        UNUserNotificationCenter.current().requestAuthorization(
            options: [.alert, .badge, .sound],
            completionHandler: { (granted, error) in
                print("access granted!")
                guard granted else { return }
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                    print("Registered: \(UIApplication.shared.isRegisteredForRemoteNotifications)")
                }
            })
        return true
    }
    // MARK: UISceneSession Lifecycle
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
}


The problem is, function "didRegisterForRemoteNotificationsWithDeviceToken" is never getting called.
I also created "Apple Push Notification service SSL Certificates" in my developer account, added "Push Notifications" to my capabilities in Xcode but it's still not working. Every time I tab on "Allow" to allow notifications, it says "access granted", but it does not print out any device token. "isRegisterForRemoteNotifications" also returns true.

I also searched on the internet for around 2-3 hours, but I couldn't find any solution yet.

I'm using Xcode 12 Beta 2 and iOS 14 Dev Beta 2.

Thank you.
assuming your code is correct and you enabled the notification capabilities, are you running on a device or in the simulator?
Facing same issue I am using Xcode 11.4 and notifications working fine on iOS 12 but not working on iOS 13. didRegister not getting called on iOS 13
Same problem, didRegisterForRemoteNotificationsWithDeviceToken called on iOS 12 but only sometimes on iOS 13. One day it works on iOS 13 the other day not. Same App, no codechanges. This is really weird. There several postings in the forum regarding this problem...
Same issue here. Intermittently working on iOS 13. When it works, I get the device token in call back didRegisterForRemoteNotificationsWithDeviceToken. Can someone help?
The only way I've found to make it work again, is to restart the device. After that it starts getting called.
Did you ever get a resolution to this? I'm experiencing the exact same issue in Xcode 12.3, iOS 14.3.
This was a known common issue during the beta which has been taken care of and is no longer an issue on the APNs side.

This is likely a temporary issue and it might resolve itself.

If you are getting stuck in the state where this isn’t working, you can try a couple things to kick start the connection again.
  1. Put the device in Airplane Mode.

  2. After a few seconds, take it out of Airplane Mode. This will cause the device to try to reestablish its persistent connection to APNs.

  3. Run your app again to request a new token

If this is not resolving the issue, you can try the same after switching to another network, if possible. Alternatively you can try switching to/from a cellular connection to see if that makes a difference.

To resolve more persistent issues, I suggest making sure that your end of the network is properly configured to allow devices access APNs.

If you use Wi-Fi behind a firewall, or using Private Access Point for cellular data, make sure you can connect to specific ports.
You need a direct, *unproxied* connection to the APNs servers on these ports:
  • TCP port 5223 to communicate with APNs.

  • TCP port 443 or 2197 to send notifications to APNs.

  • TCP port 443 is required during device activation, and afterwards for fallback (on Wi-Fi only) if devices can't reach APNs on port 5223.

The APNs servers use load balancing, so your devices don't always connect to the same public IP address for notifications. It's best to let your device access these ports on the entire 17.0.0.0/8 address block, which is assigned to Apple.

You know what's interesting in my specific case?

I can see the token being delivered when I check the logs in Console.app:


default	10:58:17.653847+0000	SpringBoard	<APSConnection: 0x600003d653b0> Received token <80ea34a3 55579e65 3486d18b bcffacb0 64427268 c96bf02e 532d4cd7 9e58759d f94b40f8 dd888069 8a24e50b 166b81e1 f684d664 bf8dded1 be9c7ee7 074fb77c 4fada1d5 69c5944c 2c43371b 228b9cb0> forTopic com.example.app.bundleid identifier 5D29AEF5-060A-4CE3-825E-1E51695E851B

But the callbacks are not being called in the app:

Neither didRegisterForRemoteNotificationsWithDeviceToken

or didFailToRegisterForRemoteNotificationsWithError.

I'm using a Development Provisioning Profile. This happens both on a real device and a Simulator, both iOS 18.2.

I am using the CapacitorJs framework to develop the application. However, it also throw the same error message (Yes it is capacitor event; however it calls apple remote notification event) :

event capacitorDidRegisterForRemoteNotifications not called

In my case, because I wrote FirebaseApp.configure() in my didFinishLaunchingWithOptions method in the app delegate file.

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

After removing this"FirebaseApp.configure()", I got my callbacks didRegisterForRemoteNotificationsWithDeviceToken

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
        let token = tokenParts.joined()
        print("Device Token: \(token)")
		sendDeviceTokenToBackend(token: token)
        // Check if token has changed
    }
'didRegisterForRemoteNotificationsWithDeviceToken' never called
 
 
Q