I have an iOS
app that uses Firebase
for remote notifications. For some reason, when testing my app, I never receive the registered fcmToken
on launch despite documentation stating:
By default, the FCM SDK generates a registration token for the client app instance on app launch. Similar to the APNs device token, this token allows you to send targeted notifications to any particular instance of your app.
I configure FirebaseApp
in didFinishLaunchingWithOptions
AppDelegate
delegate function, I set Messaging.messaging().delegate = self
and UNUserNotificationCenter.current().delegate = self
thereafter.
Neither didRegisterForRemoteNotificationsWithDeviceToken
, didFailToRegisterForRemoteNotificationsWithError
, nor didReceiveRegistrationToken
delegate functions are called.
Now, what did trigger didReceiveRegistrationToken
was when I added UIApplication.shared.registerForRemoteNotifications()
alongside the aforementioned configurations.
Is this how it was always supposed to be/should be? For some reason, I had received the token without this previously and only called registerForRemoteNotifications()
at the point I actually needed to ask the user to allow for push notifications. Now, I don’t.
Any guidance would be appreciated.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
attemptRegisterForNotification()
return true
}
private func attemptRegisterForNotification() {
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
UIApplication.shared.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("didFailToRegisterForRemoteNotificationsWithError \(error)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("didRegisterForRemoteNotificationsWithDeviceToken \(deviceToken)")
Auth.auth().setAPNSToken(deviceToken, type: .unknown)
Messaging.messaging().apnsToken = deviceToken
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
print("Firebase FCM \(fcmToken)")
}