firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.81k stars 884 forks source link

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

Open thomasmburke opened 4 years ago

thomasmburke commented 4 years ago

[REQUIRED] Describe your environment

[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:

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 commented 4 years ago

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

bjgrosse commented 4 years ago

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

mindiii006 commented 4 years ago

I am also getting this issue.

rommelpe commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

gestewa commented 4 years ago

I am also getting this issue.

thayannevls commented 4 years ago

I'm getting the same error :(

mmermerkaya commented 4 years ago

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 commented 4 years ago

Any update on this issue?

mmermerkaya commented 4 years ago

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 commented 4 years ago

Can we have the latest updates regarding this issue?

mmermerkaya commented 4 years ago

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 commented 4 years ago

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

kdluu commented 4 years ago

This is happening to me too

hsubox76 commented 4 years ago

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

CodeByBryan commented 4 years ago

Using 7.6.0 and am still having the same issue!

mmermerkaya commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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.

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 commented 4 years ago

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

Ridermansb commented 4 years ago

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

ctavan commented 4 years ago

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

CodeByBryan commented 4 years ago

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

niklat22 commented 4 years ago

Same error for user resubscribe:

FirebaseError: Messaging: A problem occured while unsubscribing the user from FCM: Internal error encountered. (messaging/token-unsubscribe-failed). (messaging/token-unsubscribe-failed). at Ik. (https://www.gstatic.com/firebasejs/7.6.0/firebase.js:1:811146) at https://www.gstatic.com/firebasejs/7.6.0/firebase.js:1:52093 at Object.next (https://www.gstatic.com/firebasejs/7.6.0/firebase.js:1:52198) at n (https://www.gstatic.com/firebasejs/7.6.0/firebase.js:1:50968)

Depender-Sethi commented 4 years ago

Waiting for the fix, for the time being I'm using a way around by checking the error code and calling the function again because this error is thrown only the first time.

const turnOnNotification = async () => {
    const uid = firebase.auth().currentUser.uid;
    const messaging = fb.messaging();
    try {
      await messaging.requestPermission();
      const token = await messaging.getToken();
      // send to server
    } catch (err) {
      if (err.code === "messaging/token-unsubscribe-failed")
        turnOnNotification();
    } 
  };
Marc-Tanne commented 4 years ago

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.

are there any new updates on this. Our company is getting held up by this issue. we are using "firebase": "7.6.1" and the error is not suppressed in this version. we are calling get token like this: messaging.getToken("SENDER ID", "FCM");

kdastan commented 4 years ago

I tried on version 7.9.1

If I grant permission to show notification, the token generates just fine, but if I remove permission from chrome settings and then try to reload the page and after grant permission again in a console I'm having error like

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

but at the same time, a new valid token generates.

rommelpe commented 4 years ago

Sorry to hear about your issue, @kdastan. Other dev seems to encounter it, but I can't get the same behavior. Can I ask you to share the following? Thanks.

akuales commented 4 years ago

I am getting the same error, but only on Chrome (Firefox works fine)... i'm using npm-firebase-version 7.13.2. The weird thing is, this issue started today - when I tested my app extensively for the last couple of days without any problems.

srameshr commented 4 years ago

@akuales Go to Application -> Service workers and unregister your service worker.

nikolas-nelson commented 4 years ago

The same problem here.

main.js:2852 Unable to get permission to notify. FirebaseError: Messaging: A problem occured while subscribing the user to FCM: Requests to this API fcmregistrations.googleapis.com method google.firebase.fcm.registration.v1.RegistrationApi.CreateRegistration are blocked. (messaging/token-subscribe-failed).

trying everything and still getting this error

haimlvov10 commented 4 years ago

The same problem here.

main.js:2852 Unable to get permission to notify. FirebaseError: Messaging: A problem occured while subscribing the user to FCM: Requests to this API fcmregistrations.googleapis.com method google.firebase.fcm.registration.v1.RegistrationApi.CreateRegistration are blocked. (messaging/token-subscribe-failed).

trying everything and still getting this error

Same here.

nikolas-nelson commented 4 years ago

I think I fixed this problem by changing credentials in the google cloud console (API restrictions)

michielmulders commented 4 years ago

@Jeybee89 Can you expand on this how you did it? The problem still persists.

nikolas-nelson commented 4 years ago

@michielmulders do to google cloud console (https://console.cloud.google.com/), select your project then APIs & Services > Credentials > API keys > Browser Key > API restrictions > Don't restrict key

manemao commented 4 years ago

@Jeybee89 Error repeated with this config too. version 7.14.1.

not causing any special problems. just ugly 404 error in console

devacc00 commented 4 years ago

Waiting for the fix, for the time being I'm using a way around by checking the error code and calling the function again because this error is thrown only the first time.

const turnOnNotification = async () => {
    const uid = firebase.auth().currentUser.uid;
    const messaging = fb.messaging();
    try {
      await messaging.requestPermission();
      const token = await messaging.getToken();
      // send to server
    } catch (err) {
      if (err.code === "messaging/token-unsubscribe-failed")
        turnOnNotification();
    } 
  };

It fixed <3 Only minus is that you sending every time existing token to server

Mwaseemzakir commented 4 years ago

issue

aforty commented 4 years ago

The same problem here.

main.js:2852 Unable to get permission to notify. FirebaseError: Messaging: A problem occured while subscribing the user to FCM: Requests to this API fcmregistrations.googleapis.com method google.firebase.fcm.registration.v1.RegistrationApi.CreateRegistration are blocked. (messaging/token-subscribe-failed).

trying everything and still getting this error

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

Screen Shot 2020-07-17 at 13 07 13

Stas-Buzunko commented 3 years ago

updating to latest version (firebase@7.21.1) fixed for me

YeetOrBeYate commented 3 years ago

@Stas-Buzunko firebase 7.21.1? like the package or in your service worker?

jonathanmv commented 3 years ago

I'm using 7.21.1 in both the package.json and the service worker. I was using the key from another project by mistake. Check that the same is not happening to you 😂

otterlistic commented 3 years ago

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.

jeffykle commented 3 years ago

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 commented 3 years ago

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

dr-aiuta commented 3 years ago

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 commented 3 years ago

do you use Vue ? @dr-aiuta

dr-aiuta commented 3 years ago

do you use Vue ? @dr-aiuta

No

arnav-kr commented 3 years ago

same issue here