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

firebase.messaging.getToken() edgecase when push notification is set back to default (ask) #2364

Open
thomasmburke opened this issue Nov 15, 2019 · 73 comments · Fixed by thematters/matters-web#865
Milestone

Comments

@thomasmburke
Copy link

@thomasmburke thomasmburke commented Nov 15, 2019

[REQUIRED] Describe your environment

  • Operating System version: macOS Mojave Version 10.14.6
  • Browser version: Google Chrome Version 78.0.3904.97
  • Firebase SDK version: 7.0.0
  • Firebase Product: Firebase Cloud Messaging

[REQUIRED] Describe the problem

The issue I have identified was introduced in version 7.0.0 of the firebase-js-sdk. I do not run into this issue in versions previous to 7.0.0. The issue occurs when a use grants notification permissions, gets a FCM token, then updates the notification permissions in the browser back to Ask (default), reloads the page, grants notification permissions again in Chrome, and get an error when trying to getToken at this point. Note: subsequent calls to getToken don't face this issue.

Steps to reproduce:

The problem occurs after taking the following steps:

  • Call Notification.requestPermission() and user grants permissions

  • Call firebase.messaging.getToken()

  • Set notification permission in browser back to Ask (default)
    image

  • Call Notification.requestPermission() and user grants permissions

  • call firebase.messaging.getToken()

  • get DELETE https://fcmregistrations.googleapis.com/v1/projects/{project_id}/regis.../{FCM_token} 500 error and this specific error from FCM: "FirebaseError: Messaging: A problem occured while unsubscribing the user from FCM: FirebaseError: Messaging: A problem occured while unsubscribing the user from FCM: Internal error encountered. (messaging/token-unsubscribe-failed). (messaging/token-unsubscribe-failed)."

  • call messaging.getToken() again

  • get new token, no error this time.

Others have encountered this issue as well on Google Groups:
https://groups.google.com/a/google.com/forum/#!msg/firebase-developer-cuf/dGU1SnJ1cz0/GMa_WvWGBwAJ

Relevant Code:

The firebase javascript messaging quickstart encounters this issue:
https://github.com/firebase/quickstart-js/tree/master/messaging

Specifically, I think the update needs to be made here:
firebase-js-sdk/packages/messaging/src/controllers/base-controller.ts
Lines: 136-137

@google-oss-bot
Copy link
Contributor

@google-oss-bot google-oss-bot commented Nov 15, 2019

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

@bjgrosse
Copy link

@bjgrosse bjgrosse commented Nov 17, 2019

I also ran into this error just as the OP described.

@mindiii006
Copy link

@mindiii006 mindiii006 commented Nov 18, 2019

I am also getting this issue.

@rommelpe
Copy link

@rommelpe rommelpe commented Nov 18, 2019

Thank for the report. I was able to reproduce this using the quick start guide and filed an internal report (b/144697421).

error-messaging

I'm just wondering if you y'all happen to consistently facing this issue. There are times that I can only reproduce it on the initial load or upon requesting permission several times. Are there any particular patterns you notice that significantly contributes to the issue (aside from what mentioned)?

@thomasmburke
Copy link
Author

@thomasmburke thomasmburke commented Nov 18, 2019

Hi rommelpe,

This issue only arises after the a reset of notifications permissions and reload of page and then works properly with an additional reloaded pages. Aside from what I mentioned there are no other patterns that contribute to the issue identified.

Thanks, and let me know if there are any other questions.

@Nox04
Copy link

@Nox04 Nox04 commented Nov 18, 2019

I'm getting the same issue when I remove the push notifications permission and reload.

@gestewa
Copy link

@gestewa gestewa commented Nov 18, 2019

I am also getting this issue.

@thayannevls
Copy link

@thayannevls thayannevls commented Nov 19, 2019

I'm getting the same error :(

@mmermerkaya
Copy link
Contributor

@mmermerkaya mmermerkaya commented Nov 21, 2019

Thanks for the report. I can consistently reproduce the behavior and I'm investigating the issue.

I'd like to ask everyone to please refrain from posting "This is happening to me too" comments and instead use the add reaction feature on the OP to add a thumbs up to issues that you consider important.

@TrackMyToken
Copy link

@TrackMyToken TrackMyToken commented Nov 30, 2019

Any update on this issue?

@mmermerkaya
Copy link
Contributor

@mmermerkaya mmermerkaya commented Dec 3, 2019

Hello, sorry for the lack of updates. Looks like the problem is in our backend, but there wasn't much progress as we've had a code freeze last week (because of black friday). I should be able to give you a better update or even a fix later this week.

@NaghamHalabi
Copy link

@NaghamHalabi NaghamHalabi commented Dec 6, 2019

Can we have the latest updates regarding this issue?

@mmermerkaya
Copy link
Contributor

@mmermerkaya mmermerkaya commented Dec 10, 2019

Hey everyone, our backend team is still investigating this. Since it's taking longer than my initial estimate, I just submitted a PR to temporarily suppress this error.

@spawnrider
Copy link

@spawnrider spawnrider commented Dec 11, 2019

Also reproduced this issue and need a fix
Using Firebase version 7.5.2

@kdluu
Copy link

@kdluu kdluu commented Dec 11, 2019

This is happening to me too

@hsubox76 hsubox76 added this to the 7.6.0 milestone Dec 12, 2019
@hsubox76
Copy link
Contributor

@hsubox76 hsubox76 commented Dec 12, 2019

The temporary change to suppress the error, mentioned by @mmermerkaya above, is in 7.6.0 which has just been released.

@CodeByBryan
Copy link

@CodeByBryan CodeByBryan commented Dec 14, 2019

Using 7.6.0 and am still having the same issue!

@mmermerkaya
Copy link
Contributor

@mmermerkaya mmermerkaya commented Dec 16, 2019

If you're only seeing the error in the console, that's normal. My PR is still logging the error in the console, but it should not stop the getToken call from completing successfully.

@CodeByBryan
Copy link

@CodeByBryan CodeByBryan commented Dec 18, 2019

I am receiving a token value when https://firebaseinstallations.googleapis.com/v1/projects/.../authTokens:generate is called, but get a "500" error when calling getToken()

@GoktuqCan
Copy link

@GoktuqCan GoktuqCan commented Dec 19, 2019

I get this error when using "127.0.0.1" instead of localhost.
BUT if i use "localhost" even the getToken() is not working. Showing CORS policy error.

  • On localhost error is(cannot getToken())
OPTIONS https://firebaseinstallations.googleapis.com/v1/projects/{project_id}/installations/{some_token}/authTokens:generate 403

Access to fetch at 'https://firebaseinstallations.googleapis.com/v1/projects/{project_id}/installations/{some_token}/authTokens:generate' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
  • On 127.0.0.1 error(getting token but shows this error) is:
DELETE https://fcmregistrations.googleapis.com/v1/projects/{project_id}/regis.../{FCM_token} 500

FirebaseError: Messaging: A problem occured while unsubscribing the user from FCM: Internal error encountered. (messaging/token-unsubscribe-failed). (messaging/token-unsubscribe-failed).

@mmermerkaya
Copy link
Contributor

@mmermerkaya mmermerkaya commented Dec 19, 2019

The CORS error looks like an unrelated issue. Can you create a new bug report for that?

@Ridermansb
Copy link

@Ridermansb Ridermansb commented Dec 22, 2019

@mmermerkaya did you created new issue for CORS problem?
I'm having the same issue.

@ctavan
Copy link

@ctavan ctavan commented Dec 22, 2019

@Ridermansb @mmermerkaya @GoktuqCan @CodeByBryan could you check if #2467 matches the issue that you are observing?

@CodeByBryan
Copy link

@CodeByBryan CodeByBryan commented Dec 23, 2019

I am having the issue in a Chrome Extension. when getToken() is called I recieve:

DELETE https://fcmregistrations.googleapis.com/v1/projects/{project_id}/regis.../{FCM_token} 500
FirebaseError: Messaging: A problem occured while unsubscribing the user from FCM: Internal error encountered. (messaging/token-unsubscribe-failed).
(messaging/token-unsubscribe-failed).<

I am not getting any CORS errors

@jeffykle
Copy link

@jeffykle jeffykle commented Jan 23, 2021

Enabling "FCM Registration API" for my google api key fixed that particular issue for me.

My key was already unrestricted and I'm experiencing the issue.

@Kamran92
Copy link

@Kamran92 Kamran92 commented Feb 21, 2021

I have the same problem 8.2.7
If someone has decided, help
image

@dr-aiuta
Copy link

@dr-aiuta dr-aiuta commented Mar 22, 2021

Actually, investigating here the error that @Kamran92 appointed, it seems that token is still valid, although the message says the "entity was not found". It returns success when sending a test message from my server. 8.2.10 here

@Kamran92
Copy link

@Kamran92 Kamran92 commented Mar 31, 2021

do you use Vue ? @dr-aiuta

@dr-aiuta
Copy link

@dr-aiuta dr-aiuta commented Mar 31, 2021

do you use Vue ? @dr-aiuta

No

@arnav-kr
Copy link

@arnav-kr arnav-kr commented Apr 13, 2021

same issue here

@sergdenisov
Copy link

@sergdenisov sergdenisov commented Apr 20, 2021

Have the same issue (404) with the 8.4.1 version. Found 2 ways to reproduce (2-nd step):

  1. Everything is OK with an app registration and getting a token.
  2. Block notifications, then allow notifications (without reloading a page) OR unregister your service worker in browser settings.
  3. Reload a page.

Screenshot 2021-04-20 at 18 49 38

@choubsovann
Copy link

@choubsovann choubsovann commented Apr 28, 2021

image
Error when i use function messaging.getToken();

@marciocamello
Copy link

@marciocamello marciocamello commented May 27, 2021

The same error here

@10eputzen
Copy link

@10eputzen 10eputzen commented Jun 2, 2021

It is horrible to always delete the Cache once this happens ...

@unfinishedCode
Copy link

@unfinishedCode unfinishedCode commented Jun 22, 2021

I tried some of the solutions listed on here and on a Stck Overflow question regarding this bug, it didnt seem to do the trick. The one solution on here causes an endless loop. The other solution is to use a lower version, which is blocked by a redirect. To solve this 'bug' I enabled 5 APIs in Google's Cloud Console, restricted my push notification key to the following APIs, followed by clearing cache. The five APIs that I restricted the key to are:

  • FCM Registration API
  • Firebase Cloud Messaging API
  • Firebase Installations API
  • Firebase Management API
  • Firebase Remote Config API

I also included a picture. I hope this helps somebody. This bug doesn't impact functionality, but its very unprofessional to see an error in the console.
Screen Shot 2021-06-22 at 10 03 37 AM

@sahejsingh
Copy link

@sahejsingh sahejsingh commented Jul 1, 2021

I ran into this issue this morning.

POST https://fcmregistrations.googleapis.com/v1/projects/my-project-id/registrations 401 (Unauthorized)
<call-stack>
index.html:630 Unable to get permission. FirebaseError: Messaging: A problem occurred while subscribing the user to FCM: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (messaging/token-subscribe-failed).
    at api.ts:67
    at tslib.es6.js:102
    at Object.next (tslib.es6.js:83)
    at r (tslib.es6.js:73)

here is how it happened

  • created client-1, got notifications working.
  • for some reason I happened to delete the client-1 from firebase console.
  • created client-2
  • copied the new firebaseConfig from console.
  • got the error now.
  • Noticed, the Web API Key is the same for both the clients. there are not restrictions on the web api key.
  • I have followed everything I could (unregister service worker)

Firebase JS Version: 8.6.8

scripts imported like so:

<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-messaging.js"></script>
var firebaseConfig = {
  // CONFIG COPIED FROM FIREBASE CONSOLE.
};

firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();


  messaging
    .getToken({ vapidKey: "MY_PUB_KEY" })
    .then((currentToken) => {
      console.log(currentToken)
    })
    .catch(function (err) {
      // handle error
    });

@zerolimits303
Copy link

@zerolimits303 zerolimits303 commented Jul 1, 2021

I ran into this issue this morning.

POST https://fcmregistrations.googleapis.com/v1/projects/my-project-id/registrations 401 (Unauthorized)
<call-stack>
index.html:630 Unable to get permission. FirebaseError: Messaging: A problem occurred while subscribing the user to FCM: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (messaging/token-subscribe-failed).
    at api.ts:67
    at tslib.es6.js:102
    at Object.next (tslib.es6.js:83)
    at r (tslib.es6.js:73)

here is how it happened

  • created client-1, got notifications working.
  • for some reason I happened to delete the client-1 from firebase console.
  • created client-2
  • copied the new firebaseConfig from console.
  • got the error now.
  • Noticed, the Web API Key is the same for both the clients. there are not restrictions on the web api key.
  • I have followed everything I could (unregister service worker)

Firebase JS Version: 8.6.8

scripts imported like so:

<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-messaging.js"></script>
var firebaseConfig = {
  // CONFIG COPIED FROM FIREBASE CONSOLE.
};

firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();


  messaging
    .getToken({ vapidKey: "MY_PUB_KEY" })
    .then((currentToken) => {
      console.log(currentToken)
    })
    .catch(function (err) {
      // handle error
    });

I also have the same problem since noon today, I have tried many ways but still can't fix it.

@omkarajagunde
Copy link

@omkarajagunde omkarajagunde commented Jul 1, 2021

I ran into this issue this morning.

POST https://fcmregistrations.googleapis.com/v1/projects/my-project-id/registrations 401 (Unauthorized)
<call-stack>
index.html:630 Unable to get permission. FirebaseError: Messaging: A problem occurred while subscribing the user to FCM: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (messaging/token-subscribe-failed).
    at api.ts:67
    at tslib.es6.js:102
    at Object.next (tslib.es6.js:83)
    at r (tslib.es6.js:73)

here is how it happened

  • created client-1, got notifications working.
  • for some reason I happened to delete the client-1 from firebase console.
  • created client-2
  • copied the new firebaseConfig from console.
  • got the error now.
  • Noticed, the Web API Key is the same for both the clients. there are not restrictions on the web api key.
  • I have followed everything I could (unregister service worker)

Firebase JS Version: 8.6.8
scripts imported like so:

<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-messaging.js"></script>
var firebaseConfig = {
  // CONFIG COPIED FROM FIREBASE CONSOLE.
};

firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();


  messaging
    .getToken({ vapidKey: "MY_PUB_KEY" })
    .then((currentToken) => {
      console.log(currentToken)
    })
    .catch(function (err) {
      // handle error
    });

I also have the same problem since noon today, I have tried many ways but still can't fix it.

Yup facing this issue since morning

@seniordev-ca
Copy link

@seniordev-ca seniordev-ca commented Jul 1, 2021

I ran into this issue this morning.

POST https://fcmregistrations.googleapis.com/v1/projects/my-project-id/registrations 401 (Unauthorized)
<call-stack>
index.html:630 Unable to get permission. FirebaseError: Messaging: A problem occurred while subscribing the user to FCM: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (messaging/token-subscribe-failed).
    at api.ts:67
    at tslib.es6.js:102
    at Object.next (tslib.es6.js:83)
    at r (tslib.es6.js:73)

here is how it happened

  • created client-1, got notifications working.
  • for some reason I happened to delete the client-1 from firebase console.
  • created client-2
  • copied the new firebaseConfig from console.
  • got the error now.
  • Noticed, the Web API Key is the same for both the clients. there are not restrictions on the web api key.
  • I have followed everything I could (unregister service worker)

Firebase JS Version: 8.6.8

scripts imported like so:

<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-messaging.js"></script>
var firebaseConfig = {
  // CONFIG COPIED FROM FIREBASE CONSOLE.
};

firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();


  messaging
    .getToken({ vapidKey: "MY_PUB_KEY" })
    .then((currentToken) => {
      console.log(currentToken)
    })
    .catch(function (err) {
      // handle error
    });

I also have the same issue - getting 401 error.
I tried to delete the project and create it again, but still not working.

@rachelrab
Copy link

@rachelrab rachelrab commented Jul 1, 2021

This also happens to me, started happening a day ago - not consistent.
Any way to workaround it?

@deivisondfs2
Copy link

@deivisondfs2 deivisondfs2 commented Jul 1, 2021

Does anyone have any updates?
Or where can we try to get a status?
I just created a new account and entered the new credentials but the error remains.

@gach87
Copy link

@gach87 gach87 commented Jul 1, 2021

I'm experiencing this problem also, created a new project and still remains.

@deivisondfs2
Copy link

@deivisondfs2 deivisondfs2 commented Jul 1, 2021

Ah this is the same issue:
#5081
Let's centralize the discussion over there.

@ChromeQ
Copy link

@ChromeQ ChromeQ commented Jul 1, 2021

@sahejsingh
Copy link

@sahejsingh sahejsingh commented Jul 1, 2021

I do have to say, I just realised that that my issue was specific to getting the token.
The error was: messaging/token-subscribe-failed, http code=401.
whereas this issue is about unsubscribe and http code=500.

@atzzCokeK
Copy link

@atzzCokeK atzzCokeK commented Jul 13, 2021

For future visitors: Goto Dev Tools -> Application -> Clear storage.

I feel like this issue only takes place when you disable notifications and enable them again. The fact that the service worker is registered again can also be a culprit in some form, but just unregistering the service worker did not solve the problem for me. My firebase version is 7.9.1, so I guess the issue still persists but it does not affect anything critically.

I tried this solution. It is fixed, but when you disable and enable the status for browser notification, it happens again.
thanks @Symasia

@jahooma
Copy link

@jahooma jahooma commented Sep 20, 2021

Hi y'all,

We've had this problem (or related ones) for a long time. I think I finally got it all working today. This is how.

I believe Firebase messaging is just buggy when you try to register your own service worker instead of providing the firebase-messaging-sw.js file.

So the fix was to create that file and then reference at the end of my own service-worker.js:

importScripts('firebase-messaging-sw.js')

Then you no longer need to pass serviceWorkerRegistration into getToken, simply call:

const token = await messaging.getToken({ vapidKey: config.publicVapidKey })

And it just works!

@Ashatanchik
Copy link

@Ashatanchik Ashatanchik commented Sep 23, 2021

I'm having exactly the same issue

FirebaseError: Messaging: A problem occurred while unsubscribing the user from FCM

Using "firebase": "8.3.2"

It seems like this issue takes place due to cache errors. So to fix it I am always clearing cache, that's pity :(

Unfortunately I don't have an ability to use firebase-messaging-sw.js, that's why @jahooma method wont work for me

@StefanosGiannakis
Copy link

@StefanosGiannakis StefanosGiannakis commented Oct 20, 2021

I'm getting this error on Chrome and not in Firefox. if that helps

@Mwamiq381
Copy link

@Mwamiq381 Mwamiq381 commented Oct 20, 2021

Have the same issue (404) with the 8.3.1 please guide me how can i fix this issue
error

@rockmandash
Copy link

@rockmandash rockmandash commented Dec 19, 2021

I'm using firebase-messaging-sw.js and still can reproduce this bug using @sergdenisov 's two method, so I think @jahooma 's method is not working.

My firebase SDK is 9.6.1

@mohammad-kazemzadeh
Copy link

@mohammad-kazemzadeh mohammad-kazemzadeh commented Mar 5, 2022

This issue still persists and only way to "temporarily" solve it is to clear storage.
firebase v9.6.7

@Gubancs
Copy link

@Gubancs Gubancs commented Apr 12, 2022

I get this error also with firebase v9.6.10 in Chrome but not in Firefox!

_FirebaseError: Messaging: A problem occurred while unsubscribing the user from FCM: FirebaseError: Messaging: A problem occurred while unsubscribing the user from FCM: Requested entity was not found. (messaging/token-unsubscribe-failed). (messaging/token-unsubscribe-failed).
at index.esm2017.js:428:29
at Generator.next ()
at asyncGeneratorStep (asyncToGenerator.js:3:1)
at _next (asyncToGenerator.js:25:1)
at _ZoneDelegate.invoke (zone.js:372:1)
at Object.onInvoke (core.mjs:25568:33)
at _ZoneDelegate.invoke (zone.js:371:1)
at Zone.run (zone.js:134:1)
at zone.js:1275:1
at ZoneDelegate.invokeTask (zone.js:406:1)

I can confirm the clearing the application storage on the developer console solves the problem temporarily, but it is not the best solution.

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

Successfully merging a pull request may close this issue.