fcm: getToken returns some JWT instead of the instance ID
Describe the bug
After updating to capacitor v3 and the current version of the fcm plugin, our call to getToken() stopped working. Instead of some FCM token it returns a JSON web token.
To Reproduce Steps to reproduce the behavior:
- Setup an app with FCM
- Call
requestPermissions()andregister()onPushNotifications - Call
FCM.getToken()
Expected behavior
We expected it to return an fcm token which looks like a random string with a colon. e.g. eFznuNk5RsqUaR33qoswb7:APA91…
Instead we got a JWT token: jwt.io
Desktop (please complete the following information):
- OS: Android
- Browser: capacitor
- Version: v3
Smartphone (please complete the following information):
- Device: AVD Emulator - Pixel 3a API 30
Additional context
Ionic:
Ionic CLI : 6.13.1 (/home/---/.npm-packages/lib/node_modules/@ionic/cli)
Ionic Framework : @ionic/angular 5.8.1
@angular-devkit/build-angular : 12.2.3
@angular-devkit/schematics : 12.2.3
@angular/cli : 12.2.3
@ionic/angular-toolkit : 4.0.0
Capacitor:
Capacitor CLI : 3.2.4
@capacitor/core : 3.2.4
Cordova:
Cordova CLI : not installed
Cordova Platforms : not available
Cordova Plugins : not available
Utility:
cordova-res : not installed
native-run (update available: 1.5.0) : 1.4.1
System:
NodeJS : v15.14.0 (/home/---/.nvm/versions/node/v15.14.0/bin/node)
npm : 7.19.0
OS : Linux 5.14
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 10
- Comments: 18 (3 by maintainers)
Commits related to this issue
- Merge pull request #1 from marcjulian/fix/android-get-token fix: migrate getToken to use FirebaseMessaging, #99 — committed to ZieClaw/fcm by ZieClaw 2 years ago
- Merge pull request #1 from cartona/fix/android-get-token fix: migrate getToken to use FirebaseMessaging, #99 — committed to cartona/capacitor-community-fcm by mabdelfattah 2 years ago
- Merge pull request #111 from marcjulian/fix/android-get-token fix: migrate getToken to use FirebaseMessaging, #99 — committed to capacitor-community/fcm by stewones 2 years ago
For this reason we use:
TL;DR: Cause of the issue is the migration of a breaking change in Firebase Android SDK 22.0 that went wrong in 18bb0a0b33bd4ed71b9b990a0153f9d06541003b via #88
Long version: It seems the problem did not arise from Capacitor3 upgrade, but from pull request #88 (precisely: commit 18bb0a0b33bd4ed71b9b990a0153f9d06541003b) where a breaking change in Firebase Android SDK 22.0 was tried to be migrated as documented. However, documentation here is a bit tricky, as migration differs depending on what you needed the token for: identifying the installation or getting an FCM token; seems like the instance id token did both jobs before. So unfortunately the wrong part of the migration guide was chosen: migration to
FirebaseInstallationsinstead ofFirebaseMessaging.My best guess is that they still might be the same but are not guaranteed to be the same? In the end, the migration needs to be done properly to
FirebaseMessaging.Capacitor 3’s push-notification plugin still uses the deprecated FirebaseInstanceId method, which is probably why the suggest to only use SDK 21.0.1 in their docs. But at least this is working, that’s why the workaround mentioned by @eljass works. But I would also really like to see that code updated to use the up-to-date solution based on
FirebaseMessagingThis way you don’t have to add additional code to Javascript/ts/tsx. Summary:
*.tsx:
AppDelegate.swift:
Pods:
Other deps: capacitor: v4.6.2 npm: @capacitor/app@4.1.1 @capacitor/ios@4.6.2 @capacitor/push-notifications@4.1.2
Info.plist: “Required background modes: Item 0: App downloads content in response to push notifications”
should be fixed by https://github.com/capacitor-community/fcm/pull/111
+1 - @eljass works for me with Android 10 but doesn’t work for Android 12 devices.
It took a while before I came up with something usable but this is what I came up with: