Push Notifications Fail After App Reinstallation
Description
Description After deleting and reinstalling the app, Firebase Cloud Messaging (FCM) push notifications fail to work until the app is quit and restarted once or twice. This issue seems to be related to the APNs token not being set before retrieving the FCM token.
Error Messages
The following errors are logged during the issue:
The operation couldn’t be completed. No APNS token specified before fetching FCM Token
APNS device token not set before retrieving FCM Token for Sender ID. Be sure to re-retrieve the FCM token once the APNS device token is set.
Declining request for FCM Token since no APNS Token specified
Steps to Reproduce
- Log in to the app and confirm that FCM notifications are working.
- Delete the app.
- Reinstall the app.
- Observe that FCM notifications do not work immediately after reinstallation.
If you quit and restart the app multiple times, the notifications begin to work.
Code Snippet This is the code used to register for remote notifications:
func registerForRemoteNotifications() {
let application: UIApplication = UIApplication.shared
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = (application.delegate) as? AppDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
}
Environment Firebase SDK Version: Upgraded from 8.8.0 to 11.6.0 Xcode 16.1 Swizzling: Enabled Push Notification Settings: Configured as per documentation Expected Behavior After deleting and reinstalling the app, push notifications should work immediately without requiring multiple restarts of the app.
Actual Behavior Push notifications fail to work immediately after reinstalling the app. The following errors are logged until the app is restarted once or twice:
The operation couldn’t be completed. No APNS token specified before fetching FCM Token
APNS device token not set before retrieving FCM Token for Sender ID
Declining request for FCM Token since no APNS Token specified
This issue did not occur with Firebase SDK version 8.8.0 but surfaced after upgrading to version 11.6.0.
Reproducing the issue
Steps to Reproduce
- Log in to the app and confirm that FCM notifications are working.
- Delete the app.
- Reinstall the app.
- Observe that FCM notifications do not work immediately after reinstallation.
If you quit and restart the app multiple times, the notifications begin to work.
Firebase SDK Version
11.6.0
Xcode Version
16.1
Installation Method
CocoaPods
Firebase Product(s)
Messaging
Targeted Platforms
iOS
Relevant Log Output
No response
If using Swift Package Manager, the project's Package.resolved
Expand Package.resolved snippet
Replace this line with the contents of your Package.resolved.
If using CocoaPods, the project's Podfile.lock
Expand Podfile.lock snippet
Replace this line with the contents of your Podfile.lock!
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Hi @JCsplash, your issue seems related to https://github.com/firebase/firebase-ios-sdk/issues/10679#issuecomment-1402776795, especially that you've recently updated from a Firebase version before the change mentioned went into effect.
You may also consider checking this troubleshooting guide.
Hey @rizafran why is v10.4.0+ still providing a non-nil fcmToken in the didReceiveRegistrationToken FCM delegate method before valid apnsToken? It seems like that value is refreshed only when an apnsToken is received but why is an invalid one being provided at app start time even before UNUserNotificationCenter.current().requestAuthorization and UIApplication.shared.registerForRemoteNotifications() are called? On that note, why before apnsToken is given, Messaging.messaging().fcmToken gives me an non-nil token that is invalid. Shouldn't this method return nil and the didReceiveRegistrationToken FCM delegate method not fire? Or can you explain why an invalid token is sent back?
The SDK doesn't return an FCM token when the APNs token is not available. When the No APNS token specified before fetching FCM Token. error is returned, SDK returns nil as the FCM token.
https://github.com/firebase/firebase-ios-sdk/blob/75cc4df26d83f251629453ffae855c248bb6db3d/FirebaseMessaging/Sources/Token/FIRMessagingTokenManager.m#L189-L192
I have installed and uninstalled several different apps multiple times, but I am still unable to reproduce the issue you reported.
Is this issue specific to your app? Can you reproduce the issue using our sample app? https://github.com/firebase/firebase-ios-sdk/tree/main/FirebaseMessaging/Apps/Sample
Hi @leojaygoogle I also get the same error No APNS token specified before fetching FCM Token. which is expected - however when I call Messaging.messaging().fcmToken I am still getting a stale token. It's the old token before I uninstalled the app, while it should be nil.
Yeah, fcmToken returns a cached token until it's refreshed. https://github.com/firebase/firebase-ios-sdk/blob/75cc4df26d83f251629453ffae855c248bb6db3d/FirebaseMessaging/Sources/Token/FIRMessagingTokenManager.m#L78-L80
If you need the current token, please follow the doc: https://firebase.google.com/docs/cloud-messaging/ios/client#fetching-the-current-registration-token
Hey @JCsplash, I'm wondering if you're still encountering the issue?
Hey @JCsplash. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
Since there haven't been any recent updates here, I am going to close this issue.
@JCsplash if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.