Ad

Firebase IOS Push Notifications Not Working On First Install

When I first install and open the app and accept the notifications permission alert from Apple, I get this log from Firebase:

5.16.0 - [Firebase/InstanceID][I-IID023004] Could not update attributes of the key pair to be accessible after first unlock. update status: -25300

After that, if I close or send the app to the background I don't receive any notifications. If I open the app a second time, then notifications start working normally.

This is my current setup:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    setupPushNotificationsHandling(application)
    return true
}

private func setupPushNotificationsHandling(_ application: UIApplication) {
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, _) in
        guard granted else { return }

        DispatchQueue.main.async {
            application.registerForRemoteNotifications()

            FirebaseApp.configure()
            InstanceID.instanceID().instanceID { (result, _) in
                // Receive notifications from the "all" topic
                Messaging.messaging().subscribe(toTopic: "all")
            }
        }
    }
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("response \(response)")
    completionHandler()
}
Ad

Answer

This is how I solved it.

  1. Before attempting to subscribe to a notifications topic, I wait until the delegate method application:didRegisterForRemoteNotificationsWithDeviceToken: is called.
  2. I retry until the call InstanceID.instanceID().instanceID returns a valid device token.

Complete setup:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    setupPushNotificationsHandling(application)
    return true
}

private func setupPushNotificationsHandling(_ application: UIApplication) {
    FirebaseApp.configure()

    application.registerForRemoteNotifications()

    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (_, _) in }
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Receive notifications from the "all" topic
    subscribeToNotificationsTopic(topic: "all")
}

func subscribeToNotificationsTopic(topic: String) {
    // Retry until the notifications subscription is successful
    DispatchQueue.global().async {
        var subscribed = false
        while !subscribed {
            let semaphore = DispatchSemaphore(value: 0)

            InstanceID.instanceID().instanceID { (result, error) in
                if let result = result {
                    // Device token can be used to send notifications exclusively to this device
                    print("Device token \(result.token)")

                    // Subscribe
                    Messaging.messaging().subscribe(toTopic: topic)

                    // Notify semaphore
                    subscribed = true
                    semaphore.signal()
                }
            }

            // Set a 3 seconds timeout
            let dispatchTime = DispatchTime.now() + DispatchTimeInterval.seconds(3)
            _ = semaphore.wait(timeout: dispatchTime)
        }
    }
}
Ad
source: stackoverflow.com
Ad