firebase-ios-sdk: Cannot send push notifications to iOS 13 devices (even with official example from quickstart repo)

[REQUIRED] Step 1: Describe your environment

  • Xcode version: 11.4.1
  • Firebase SDK version: 6.23.0
  • Firebase Component: Messaging
  • Component version: 4.3.1
  • Installation method: CocoaPods

[REQUIRED] Step 2: Describe the problem

Steps to reproduce:

Hi,

I cannot receive push notifications from Firebase at all. I did all necessary setup and I also double checked my code with a previous working project and it seems like I’ve reached a dead end. Tried both certificates and APN keys, double checked for typos and I couldn’t find anything wrong. Then it crossed my mind to copy paste the code from quickstart repo and same result. Important to mention is that I’ve tested the APN token with an app (Pushever) and also on online websites and it seems I’ve managed to receive a push notification on the APN token alone. Also, in the code, the FCM token is generated in the same way as in my previous working project used as an example but I think the problem lies in the FCM token received from Firebase.

iOS project target: 13.0 I’ve updated to the newest Xcode only at the end of the day and it didn’t work previously either.

Relevant Code:

–PushNotificationsManager class

internal class PushNotificationsManager: NSObject {

// MARK: - Properties -

private let userService = ServiceFactory.userService()

// MARK: - Public Methods -

func registerForPushNotifications(on viewController: UIViewController & AlertPresenter) {
    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
    
    self.setDelegates()
    
    UNUserNotificationCenter.current().requestAuthorization (
        options: authOptions,
        completionHandler: { authorized, error in
            
            guard authorized else {
                DispatchQueue.main.async {
                    viewController.showAreYouSureAlert(title: "You don't have permission for push notifications!", message: "Please update it on Settings > App > Notifications.", yesMessage: Constants.SETTINGS) { _ in
                        UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
                    }
                }

                return
            }
        
            if error == nil {
                DispatchQueue.main.async {
                    InstanceID.instanceID().instanceID { (result, error) in
                        if let error = error {
                            print("Error fetching remote instance ID: \(error)")
                        } else if let result = result {
                            print("Remote instance ID token: \(result.token)")
                        }
                    }
                    
                    UIApplication.shared.registerForRemoteNotifications()
                    
                    self.updateFirestorePushTokenIfNeeded()
                    
                    App.userDefaults.set(true, forKey: "registeredAPN")
                }
            }
        })
}

func arePushNotificationsAuthorized(completion: @escaping (_ authorized: Bool) -> Void) {
    let center = UNUserNotificationCenter.current()
    center.getNotificationSettings { (settings) in
        completion(settings.authorizationStatus == .authorized)
    }
}

func unregisterForPushNotifications() {
    UIApplication.shared.unregisterForRemoteNotifications()
}

func arePushNotificationsEnabled() -> Bool {
    return UIApplication.shared.isRegisteredForRemoteNotifications
}

func setDelegates() {
    Messaging.messaging().delegate = self
    UNUserNotificationCenter.current().delegate = self
}

}

// MARK: - Private Methods -

private extension PushNotificationsManager { func updateFirestorePushTokenIfNeeded() { if let token = Messaging.messaging().fcmToken, let userId = App.authController.currentUID {
self.userService.update(field: “fcmToken”, value: token, id: userId) { error in print(“Error: (error)”) } } } }

// MARK: - MessagingDelegate methods -

extension PushNotificationsManager: MessagingDelegate { func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { self.updateFirestorePushTokenIfNeeded() } }

// MARK: - UNUserNotificationCenterDelegate methods -

extension PushNotificationsManager: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print(“response: (response)”) }

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("kind of notification: \(notification.request.content.title)")
    completionHandler([.alert, .sound, .badge])
}

}

— relevant AppDelegate methods

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { FirebaseApp.configure() … pushNotificationsManager.setDelegates() }

the registerForPushNotifications method is called after login result and the didReceiveRegistrationToken is called after every app launch.

If you need more details please let me know.

Thank you so much for your attention!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

Now it’s working just fine. Thank you!