Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push Notifications Fail After App Reinstallation #14246

Open
JCsplash opened this issue Dec 12, 2024 · 6 comments
Open

Push Notifications Fail After App Reinstallation #14246

JCsplash opened this issue Dec 12, 2024 · 6 comments
Assignees

Comments

@JCsplash
Copy link

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

  1. Log in to the app and confirm that FCM notifications are working.
  2. Delete the app.
  3. Reinstall the app.
  4. 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

  1. Log in to the app and confirm that FCM notifications are working.
  2. Delete the app.
  3. Reinstall the app.
  4. 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!
@google-oss-bot
Copy link

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@rizafran
Copy link
Contributor

Hi @JCsplash, your issue seems related to #10679 (comment), 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.

@JCsplash
Copy link
Author

JCsplash commented Dec 13, 2024

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?

@leojaygoogle
Copy link
Contributor

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.

NSError *missingAPNSTokenError =
[NSError messagingErrorWithCode:kFIRMessagingErrorCodeMissingDeviceToken
failureReason:@"No APNS token specified before fetching FCM Token"];
handler(nil, missingAPNSTokenError);

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

@JCsplash
Copy link
Author

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.

@leojaygoogle
Copy link
Contributor

Yeah, fcmToken returns a cached token until it's refreshed.

FIRMessagingTokenInfo *cachedTokenInfo =
[self cachedTokenInfoWithAuthorizedEntity:self.fcmSenderID
scope:kFIRMessagingDefaultTokenScope];

If you need the current token, please follow the doc: https://firebase.google.com/docs/cloud-messaging/ios/client#fetching-the-current-registration-token

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants