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

Messaging.messaging().token returns errors #10679

Closed
RobinCaroff opened this issue Jan 13, 2023 · 64 comments
Closed

Messaging.messaging().token returns errors #10679

RobinCaroff opened this issue Jan 13, 2023 · 64 comments

Comments

@RobinCaroff
Copy link

Description

It has been two days since that last Messaging.messaging().token success with our app.

It currently returns the following error :

Error Domain=com.google.fcm Code=0 "Invalid fetch response, expected 'token' or 'Error' key" UserInfo={NSLocalizedFailureReason=Invalid fetch response, expected 'token' or 'Error' key}

Here is how we retrieve the token :

self.getMessaging().token { token, error in
              if let error = error {
                Logger.shared.log(.app, .error, "Error fetching FCM registration token: \(error)")
                observer(.failure(error))
              } else if let token = token {
                Logger.shared.log(.app, .debug, "FCM registration token: \(token)")
                observer(.success(token))
              }
            }
            return Disposables.create()

Note 1: the live app has not been updated since 2 weeks and we reproduce the same issue with oldest version.

Note 2: We have a different target using the same code with a different bundle id which does not have the issue.

Reproducing the issue

No response

Firebase SDK Version

10.3.0

Xcode Version

14.1

Installation Method

CocoaPods

Firebase Product(s)

Messaging

Targeted Platforms

iOS

Relevant Log Output

Error Domain=com.google.fcm Code=0 "Invalid fetch response, expected 'token' or 'Error' key" UserInfo={NSLocalizedFailureReason=Invalid fetch response, expected 'token' or 'Error' key}

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
PODS:
  - Alamofire (5.6.4)
  - Differentiator (5.0.0)
  - Firebase/AnalyticsWithoutAdIdSupport (10.3.0):
    - Firebase/CoreOnly
    - FirebaseAnalytics/WithoutAdIdSupport (~> 10.3.0)
  - Firebase/CoreOnly (10.3.0):
    - FirebaseCore (= 10.3.0)
  - Firebase/Crashlytics (10.3.0):
    - Firebase/CoreOnly
    - FirebaseCrashlytics (~> 10.3.0)
  - Firebase/Messaging (10.3.0):
    - Firebase/CoreOnly
    - FirebaseMessaging (~> 10.3.0)
  - FirebaseAnalytics/WithoutAdIdSupport (10.3.0):
    - FirebaseCore (~> 10.0)
    - FirebaseInstallations (~> 10.0)
    - GoogleAppMeasurement/WithoutAdIdSupport (= 10.3.0)
    - GoogleUtilities/AppDelegateSwizzler (~> 7.8)
    - GoogleUtilities/MethodSwizzler (~> 7.8)
    - GoogleUtilities/Network (~> 7.8)
    - "GoogleUtilities/NSData+zlib (~> 7.8)"
    - nanopb (< 2.30910.0, >= 2.30908.0)
  - FirebaseCore (10.3.0):
    - FirebaseCoreInternal (~> 10.0)
    - GoogleUtilities/Environment (~> 7.8)
    - GoogleUtilities/Logger (~> 7.8)
  - FirebaseCoreInternal (10.3.0):
    - "GoogleUtilities/NSData+zlib (~> 7.8)"
  - FirebaseCrashlytics (10.3.0):
    - FirebaseCore (~> 10.0)
    - FirebaseInstallations (~> 10.0)
    - GoogleDataTransport (~> 9.2)
    - GoogleUtilities/Environment (~> 7.8)
    - nanopb (< 2.30910.0, >= 2.30908.0)
    - PromisesObjC (~> 2.1)
  - FirebaseInstallations (10.3.0):
    - FirebaseCore (~> 10.0)
    - GoogleUtilities/Environment (~> 7.8)
    - GoogleUtilities/UserDefaults (~> 7.8)
    - PromisesObjC (~> 2.1)
  - FirebaseMessaging (10.3.0):
    - FirebaseCore (~> 10.0)
    - FirebaseInstallations (~> 10.0)
    - GoogleDataTransport (~> 9.2)
    - GoogleUtilities/AppDelegateSwizzler (~> 7.8)
    - GoogleUtilities/Environment (~> 7.8)
    - GoogleUtilities/Reachability (~> 7.8)
    - GoogleUtilities/UserDefaults (~> 7.8)
    - nanopb (< 2.30910.0, >= 2.30908.0)
  - GoogleAppMeasurement/WithoutAdIdSupport (10.3.0):
    - GoogleUtilities/AppDelegateSwizzler (~> 7.8)
    - GoogleUtilities/MethodSwizzler (~> 7.8)
    - GoogleUtilities/Network (~> 7.8)
    - "GoogleUtilities/NSData+zlib (~> 7.8)"
    - nanopb (< 2.30910.0, >= 2.30908.0)
  - GoogleDataTransport (9.2.0):
    - GoogleUtilities/Environment (~> 7.7)
    - nanopb (< 2.30910.0, >= 2.30908.0)
    - PromisesObjC (< 3.0, >= 1.2)
  - GoogleUtilities/AppDelegateSwizzler (7.11.0):
    - GoogleUtilities/Environment
    - GoogleUtilities/Logger
    - GoogleUtilities/Network
  - GoogleUtilities/Environment (7.11.0):
    - PromisesObjC (< 3.0, >= 1.2)
  - GoogleUtilities/Logger (7.11.0):
    - GoogleUtilities/Environment
  - GoogleUtilities/MethodSwizzler (7.11.0):
    - GoogleUtilities/Logger
  - GoogleUtilities/Network (7.11.0):
    - GoogleUtilities/Logger
    - "GoogleUtilities/NSData+zlib"
    - GoogleUtilities/Reachability
  - "GoogleUtilities/NSData+zlib (7.11.0)"
  - GoogleUtilities/Reachability (7.11.0):
    - GoogleUtilities/Logger
  - GoogleUtilities/UserDefaults (7.11.0):
    - GoogleUtilities/Logger
  - IQKeyboardManager (6.5.10)
  - Kingfisher (7.4.1)
  - Moya (15.0.0):
    - Moya/Core (= 15.0.0)
  - Moya/Core (15.0.0):
    - Alamofire (~> 5.0)
  - Moya/RxSwift (15.0.0):
    - Moya/Core
    - RxSwift (~> 6.0)
  - nanopb (2.30909.0):
    - nanopb/decode (= 2.30909.0)
    - nanopb/encode (= 2.30909.0)
  - nanopb/decode (2.30909.0)
  - nanopb/encode (2.30909.0)
  - NSLogger/ObjC (1.9.7)
  - NSLogger/Swift (1.9.7):
    - NSLogger/ObjC
  - PromisesObjC (2.1.1)
  - RxBlocking (6.5.0):
    - RxSwift (= 6.5.0)
  - RxCocoa (6.5.0):
    - RxRelay (= 6.5.0)
    - RxSwift (= 6.5.0)
  - RxDataSources (5.0.0):
    - Differentiator (~> 5.0)
    - RxCocoa (~> 6.0)
    - RxSwift (~> 6.0)
  - RxGesture (4.0.4):
    - RxCocoa (~> 6.0)
    - RxSwift (~> 6.0)
  - RxRelay (6.5.0):
    - RxSwift (= 6.5.0)
  - RxSwift (6.5.0)
  - RxSwiftExt (6.0.1):
    - RxSwiftExt/Core (= 6.0.1)
    - RxSwiftExt/RxCocoa (= 6.0.1)
  - RxSwiftExt/Core (6.0.1):
    - RxSwift (~> 6.0)
  - RxSwiftExt/RxCocoa (6.0.1):
    - RxCocoa (~> 6.0)
    - RxSwiftExt/Core
  - RxTest (6.5.0):
    - RxSwift (= 6.5.0)
  - SwiftLint (0.49.1)
  - SwiftyAttributes (5.1.1)

DEPENDENCIES:
  - Firebase/AnalyticsWithoutAdIdSupport
  - Firebase/Crashlytics
  - Firebase/Messaging
  - IQKeyboardManager (~> 6.5.9)
  - Kingfisher (~> 7.4.1)
  - Moya (~> 15.0.0)
  - Moya/RxSwift (~> 15.0.0)
  - NSLogger/Swift
  - RxBlocking (~> 6.5.0)
  - RxCocoa (~> 6.5.0)
  - RxDataSources (~> 5.0.0)
  - RxGesture (~> 4.0.4)
  - RxSwift (~> 6.5.0)
  - RxSwiftExt (~> 6.0.1)
  - RxTest (~> 6.5.0)
  - SwiftLint (~> 0.49.1)
  - SwiftyAttributes (~> 5.1.1)

SPEC REPOS:
  https://github.com/CocoaPods/Specs.git:
    - Alamofire
    - Differentiator
    - Firebase
    - FirebaseAnalytics
    - FirebaseCore
    - FirebaseCoreInternal
    - FirebaseCrashlytics
    - FirebaseInstallations
    - FirebaseMessaging
    - GoogleAppMeasurement
    - GoogleDataTransport
    - GoogleUtilities
    - IQKeyboardManager
    - Kingfisher
    - Moya
    - nanopb
    - NSLogger
    - PromisesObjC
    - RxBlocking
    - RxCocoa
    - RxDataSources
    - RxGesture
    - RxRelay
    - RxSwift
    - RxSwiftExt
    - RxTest
    - SwiftLint
    - SwiftyAttributes

SPEC CHECKSUMS:
  Alamofire: 4e95d97098eacb88856099c4fc79b526a299e48c
  Differentiator: e8497ceab83c1b10ca233716d547b9af21b9344d
  Firebase: f92fc551ead69c94168d36c2b26188263860acd9
  FirebaseAnalytics: 036232b6a1e2918e5f67572417be1173576245f3
  FirebaseCore: 988754646ab3bd4bdcb740f1bfe26b9f6c0d5f2a
  FirebaseCoreInternal: 29b76f784d607df8b2a1259d73c3f04f1210137b
  FirebaseCrashlytics: f20d956f8229010b645e534693c39e0b7843c268
  FirebaseInstallations: e2f26126089dcf41e215f7b8925af8d953c7d602
  FirebaseMessaging: e345b219fd15d325f0cf2fef28cb8ce00d851b3f
  GoogleAppMeasurement: c7d6fff39bf2d829587d74088d582e32d75133c3
  GoogleDataTransport: 1c8145da7117bd68bbbed00cf304edb6a24de00f
  GoogleUtilities: c2bdc4cf2ce786c4d2e6b3bcfd599a25ca78f06f
  IQKeyboardManager: 45a1fa55c1a5b02c61ac0fd7fd5b62bb4ad20d97
  Kingfisher: cd762a593a61b2fbecf7645c00f9a801a3ebfc9c
  Moya: 138f0573e53411fb3dc17016add0b748dfbd78ee
  nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431
  NSLogger: cf3013a4fba189f631f07e6c3d5b8ce2b209654b
  PromisesObjC: ab77feca74fa2823e7af4249b8326368e61014cb
  RxBlocking: 04b5fd28bb5ea49f7d64b80db8bbfe50d9cc1c7d
  RxCocoa: 94f817b71c07517321eb4f9ad299112ca8af743b
  RxDataSources: aa47cc1ed6c500fa0dfecac5c979b723542d79cf
  RxGesture: f3efb47ed2d26a8082f7b660d4a59970e275a7f8
  RxRelay: 1de1523e604c72b6c68feadedd1af3b1b4d0ecbd
  RxSwift: 5710a9e6b17f3c3d6e40d6e559b9fa1e813b2ef8
  RxSwiftExt: d61c4b9b06e0a448041b1c659e0f1b5c5a035afc
  RxTest: eb2d23adefc5a5ebf5779c7792fa3edfe6ebcc17
  SwiftLint: 32ee33ded0636d0905ef6911b2b67bbaeeedafa5
  SwiftyAttributes: e3e13891ddac3c62b3d7798d5c3969cea88e62bb

PODFILE CHECKSUM: e2e890938470dccb8e731dbb945147be1c8c971c

COCOAPODS: 1.11.3

@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.

@RobinCaroff
Copy link
Author

@tomwanzek
Copy link

@RobinCaroff Thanks for opening this one and cross-referencing 🙇

While not directly related, it seems contextually interesting that we have started seeing some other previously non-existent behaviors with the Firebase Messaging SDK/React Native Firebase Messaging.

We were doing some regression testing for an upgrade of our RN Instabug integration in the App, and encountered another errors on iOS, which we had never seen "surfaced" to our error reporting:

  • [messaging/unknown] The Internet connection appears to be offline. This message was logged as part of a scenario were we deliberately took down the network (router offline for Wifi). Historically, I suspect, this might have been handled "silently" by the Messaging SDK?

Again, just for context if that helps narrow down how the messaging.getToken() issue made it into a codebase that had not updated Firebase dependencies when the error started to occur.

@henryadebayo
Copy link

having the same issue as @RobinCaroff how do i resolve it ?

@flaviodsilverio
Copy link

I'm having the same issues, any tips on how to solve it?

@rizafran
Copy link
Contributor

Hi all, I tried to reproduce the issue but I was able to retrieve the token successfully. With this, could you share a minimal repro app as well as the complete stack trace or app debug logs for further investigation?

@RobinCaroff
Copy link
Author

Hi all, I tried to reproduce the issue but I was able to retrieve the token successfully. With this, could you share a minimal repro app as well as the complete stack trace or app debug logs for further investigation?

Hi @rizafran . Thank you for your answer. What's really strange is that I also use the same codebase with a different bundle id (my preprod environment) and I don't get the error. I checked the Firebase plist but it is up to date.

I'll try to produce a minimal repo where I can reproduce the error. In the meantime, is there any specific Firebase log that I could provide to help you narrow down the issue?

@boseidel
Copy link

Also ran into this. I have tried various versions of the firebase pod during the installation of the plugin and still facing the error on emulators as well as production environments.

@flaviodsilverio
Copy link

flaviodsilverio commented Jan 17, 2023

What's really strange is that I also use the same codebase with a different bundle id (my preprod environment) and I don't get the error. I checked the Firebase plist but it is up to date.

This makes it even more curious, since the same exact thing is happening to me. In one of the environments it works flawlessly, the other is failing constantly!

@fiizzy
Copy link

fiizzy commented Jan 17, 2023

Having the same issue. No idea what's happening. Started all of a sudden.

@XavierMF
Copy link

Same issue here. We notice an error spike on firebase crashlytics as we send the error
Capture d’écran 2023-01-17 à 22 25 56

@fiizzy
Copy link

fiizzy commented Jan 17, 2023

Can we say this is a firebase thingy for sure?

@Seanmclem
Copy link

Seanmclem commented Jan 18, 2023

Maybe Certs are expired...the Apple APN keys are setup, and my Dev and Prod ones are invalid. One in expired last month in December, and the prod one expired Today.

This was the exact error I was getting. the expected 'token' or 'Error' key one.

EDIT: didn't fix it

@henryadebayo
Copy link

@Seanmclem chaged APNs key Ali but it did not fix the issue maybe it may work you

@Seanmclem
Copy link

@henryadebayo Just tried it too, and no change either.

@yeahse
Copy link

yeahse commented Jan 18, 2023

So serious problem

@henryadebayo
Copy link

henryadebayo commented Jan 18, 2023

@Seanmclem and @yeahse so i removed the GoogleServices-info.plist file from my Runner folder, then i did flutter clean and clean build folder then flutter pub get and it worked, yet to replace the GoogleServices-info.plist file , i i'm thinking the GoogleServices-info.plist file is corrupt don't know how or if this ever happens will replace it and let you know if it still works

@Seanmclem
Copy link

Seanmclem commented Jan 18, 2023

@henryadebayo I was able to fix it based on your reply, so thanks! However, I was thinking about the plist thing -and felt like it was unlikely that all of our GoogleServices-info.plist files are suddenly corrupt in the past 5 days. So rather than deleting it -I did the other part you mentioned. I stopped and restarted my app. Not even a full rebuild, just a new JS bundle (I'm using React Native), so a quick stop and restart, also closed and re-opened simulator.

The only other thing we have in common is we both just re-added our APN certificates. So that might be required too, but afterwards restarting the build was definitely the final thing that fixed it for me.

@redccoma
Copy link

redccoma commented Jan 18, 2023

We are using Firebase Unity SDK 9.40, which wraps Firebase iOS SDK, and we are experiencing the same problem as the issue in this article, so we leave an opinion.

In the past week, 2 out of 15 apps in service were only on iOS.

  • All apps use the same code
  • App crash occurs while the app starts with the error described in this article

The method we implemented was implemented in items 1 and 2 below.

  1. Obtain a token by registering an event like the guide at https://firebase.google.com/docs/cloud-messaging/unity/client#initialize

  2. If the token was not obtained in method 1, acquire the token by additionally implementing the https://stackoverflow.com/a/69222193/13368553 method

In one app, the app crash was fixed by removing 2, but in the other app, even if 2 was removed, it was not fixed, so the code implemented in 1 was deleted to respond.

Today, we're facing the same issue with the rest of the apps, and we're going to respond the same way we finally responded.

However, if you respond in this way, you will not be able to use FCM, so you need to respond quickly.

@yeahse
Copy link

yeahse commented Jan 18, 2023

@Seanmclem and @yeahse so i removed the GoogleServices-info.plist file from my Runner folder, then i did flutter clean and clean build folder then flutter pub get and it worked, yet to replace the GoogleServices-info.plist file , i i'm thinking the GoogleServices-info.plist file is corrupt don't know how or if this ever happens will replace it and let you know if it still works

I just followed your solution and it does not work

@XavierMF
Copy link

An update has been made to the Firebase SDK to provide an APN token at launch on the latest SDK release (https://firebase.google.com/support/release-notes/ios)
Obviously, this modification applies even if we are not using the latest version of the SDK. This also applies to older versions which should not be the case....

@Monsteel
Copy link

@XavierMF How can I follow this method? I saw the document, but I don't know.

@XavierMF
Copy link

If you have an APN token (user has accepted push notification), it must be forwarded to firebase sdk. And then you should be able to retrieve a firebase token.
However, if you use this token not only for enable push notification user, you won't be able to retrieve a token and that is the problem. If you use older version of the SDK, this rules apply also... It shouldn't be the cause and that's why this is a Firebase issue from my point of view

@DanielReyesDev
Copy link

@aashishpatil-g , our issue (#10710) has gone after rollback.

@adirgan
Copy link

adirgan commented Jan 19, 2023

@aashishpatil-g What they have to do is that only SDK 10.4.0 and above is the one that does not allow obtaining the FCM token without the APN, so that this change has to be implemented from that version, because applying it to everyone is not ethical

In my case the problem is already solved after rollback, but I will update to 10.4.0 with the new way they ask to obtain the FCM Token

@aashishpatil-g
Copy link
Contributor

@DanielReyesDev - thanks for confirming that the issue was resolved.

@adirgan - thanks for the feedback.

@codewithdo
Copy link

It worked, thanks!

@raulnoz
Copy link

raulnoz commented Jan 19, 2023

Solved here

@XavierMF
Copy link

Up here also. @aashishpatil-g if you can give us more infos about the case of this evolution will be live again. Which SDK version, when, etc...
However thank you for reactivity

@sveyp
Copy link

sveyp commented Jan 19, 2023

Solved as well! Thanks!

@RobinCaroff
Copy link
Author

It works also on my app. Thanks !

@Tahir-7cTech
Copy link

Thanks issue is fixed

@mikehardy
Copy link
Contributor

mikehardy commented Jan 19, 2023

I verify that things work now with firebase-ios-sdk for version 10.3.0 after rolling back the server change (thank you! That takes the pressure off us over at react-native-firebase, we appreciate it), but I still show an issue when attempting to qualify firebase-ios-sdk 10.4.0

Specifically, I see this when our e2e test suite exercises the Obj-C API and makes a call to deleteToken:

The operation couldn’t be completed. No APNS token specified before fetching FCM Token

Okay, that appears to be somewhat expected based on the discussion in this issue and in the related changelog for 10.4.0 firebase-ios-sdk release, however, what I am personally missing (and thus cannot fix for the react-native-firebase community) is some guidance on exactly why the APNS token might be null (despite swizzling being in effect...) and how to forcibly fetch (or re-fetch) an APNS token in order to allow further work with FCM tokens

In our test app we are registered for remote notifications, so we should receive APNS tokens, and swizzling is on, so firebase-ios-sdk should be receiving the APNS tokens for messaging. I'm not sure what else we need to do to handle this change and adopt 10.4.0 but would love a hint

Thanks

[Update: our native code had conditionals that detected simulator and cut-out from the remote notification registration / APNS token fetch flows, so it would never get an APNS token of course! On an M1 mac with macOS 13 + iOS 16 Simulator we can get APNS tokens and everything works even on Simulator now when those conditions are met. We will fake an APNS token for other Simulators, and try to improve dev experience with better messaging for the other cases where APNS token may not be available]

@suyuti
Copy link

suyuti commented Jan 19, 2023

It works as expected now.

@aashishpatil-g
Copy link
Contributor

aashishpatil-g commented Jan 24, 2023

Following up with some guidance for the Firebase 10.4.0 SDK.

As stated in the release notes, 10.4.0 onwards, we are declining vending a FCM token until an APNS token is provided. We have always been logging a WARNING when this has occurred in previous versions of the SDK but haven’t declined to provide a FCM token. Such a vended FCM token is not useful since it is missing a target APNS token and thus has no endpoint to send the pushes to.

Scenarios:

  1. On a fresh install of the app,
    Firebase configure flow triggers the FCM register flow even though there isn’t a APNS token. You will see an error message due to this flow. This error can be ignored as the FCM token is refetched once an APNS token is provided. I am investigating if this can be fixed in a future SDK release.

UPDATE Mar 1, 2023: This has been fixed in 10.6.0 - #10789

  1. For Objective C or Swift (not SwiftUI) projects, if swizzling is enabled,
    FCM token refetch happens automatically when we get the APNS token. The swizzling will take care of listening for when APNS token is available. The FCM messaging delegate callback will be called once an FCM token is available.

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?)

  1. If swizzling is disabled or if your project is pure SwiftUI (swizzling does not work well with SwiftUI projects)
    You must implement the UIApplicationDelegate callbacks to listen for APNS token availability. Check the SwiftUI sample in the FCM SDK codebase for how to achieve this.

  2. Testing: If you have unit or integration tests running in the simulator, it is not possible to get an APNS token unless specific environment is used - iOS 16 Simulator + macOS 13 + Apple Silicon HW. If that environment condition is not met, there isn’t an APNS token available in iOS simulator. As a result, any tests for fetching FCM token will error out in the Firebase 10.4.0 SDK. Either reconsider the tests or try to provide fake APNS token for test purposes only. If you provide a fake APNS token, be sure to delete any FCM token created from this process. Example - the FIRMessagingPubSubTest in the Firebase SDK uses a fake APNS token.

@kiishhan
Copy link

I have solved this issue by mentioning a specific version of the firebase core in Podfile and the rest of the firebase pods will adopt the respective version which will convenient to them.

pod 'Firebase/Core', '8.12.1'

Remove these files from the project:

  • Podfile.lock
  • Pods [Directory]
  • .xcworkspace

and just go with the "pod install".
That's it.

@mikehardy
Copy link
Contributor

But... @kishanbarmawala this reads the same as "I have decided to continue creating invalid FCM tokens in our project, by permanently relying on outdated versions of the software" - I mean that in a technical sense, I believe those are technically valid substitutions in your statement. I totally understand if it is just deferring the maintenance to a future date when project timelines allow but I would be careful adopting that version pin permanently...

@aashishpatil-g
Copy link
Contributor

The issue identified in bullet 1 in the comment has been fixed in 10.6.0 - https://firebase.google.com/support/release-notes/ios#fcm

I will close this issue since there aren't any opens.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.