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

getInstanceIdToken should not report messaging/token-subscribe-failed exceptions #4909

Closed
sudkumar opened this issue May 14, 2021 · 5 comments

Comments

@sudkumar
Copy link

sudkumar commented May 14, 2021

Description

I was successfully using @firebase/functions in my application and recently added @firebase/messaging. Everything works in development environment but when pushed to staging, I am getting messaging/token-subscribe-failed error in my existing flows which were working with @firebase/functions.

It seemed like functions module is somehow accessing messaging module. After a close inspection and debugging, I found that getInstanceIdToken is calling messaging.getToken.

async getInstanceIdToken(): Promise<string | undefined> {
if (
!this.messaging ||
!('Notification' in self) ||
Notification.permission !== 'granted'
) {
return undefined;
}
try {
return this.messaging.getToken();
} catch (e) {
// We don't warn on this, because it usually means messaging isn't set up.
// console.warn('Failed to retrieve instance id token.', e);
// If there's any error when trying to get the token, leave it off.
return undefined;
}
}

I am not fully aware of the reasoning behind it but after reading the following comments

// We don't warn on this, because it usually means messaging isn't set up.

, I found an issue here. When I checked the control flow of this function. It looks like the control flow will never enter into the catch block because the return value of messaging.getToken is a pending promise.

Screenshots for Error Messages

Error Message

Console.error

Environment Configuration

  • Any Browser
  • Firebase/JS v8.2.1
  • Integrated functions and messaging services
  • Hosting on custom domains
  • Notifications Permission is "Granted"
    • Customer initially click on "Notify Me" feature of app and subscribed to notifications
    • Granted notifications permission
    • [Important] Clicked on "Notify Me" again and unsubscribed from the notifications
    • [Important] Notifications permission is still set to "granted" in the browser
    • Refresh the page before testing

Reproduce the Error

Once can directly identify this issue in getInstanceIdToken method of functions module with a simple control flow analysis.

Here are some step to reproduce this inside an application

  1. Create a firebase Web App with any starter kit.
  2. Integrate @firebase/functions module and call any httpsCallable('<your_function>') for your implementation
  3. Please test that it is working as expected
  4. Now integrate the @firebase/messaging module following this tutorial
  5. Preview the application in the web browser and grant permissions for Notifications.
  6. Now test your cloud function from step 2 (httpsCallable('<your_function>')) again. It will start throwing messaging/token-subscribe-failed. It works on your local development environment (as it was in my case), please deploy it to a custom domain (non-firebase).

Suggest a Fix

This is a minor issue with the code and can be fixed with a simple await statement before messaging.getToken call

 try { 
-   return this.messaging.getToken(); 
+   return await this.messaging.getToken();
} catch (e) { 

Here, the await will ensure that exceptions go to the catch statement.

I am happy to contribute with a PR if this qualifies as an issue.


Please let me know if you need any more information regarding this issue. Also, please direct me if I am doing some wrong here.

@google-oss-bot
Copy link
Contributor

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

@sudkumar
Copy link
Author

@looptheloop88 Can you please assign someone to look into it?

I am happy to create a PR if this is approved as an issue.

Thanks you.

@zwu52
Copy link
Member

zwu52 commented May 18, 2021

Fix will be rolled out this or next week.

@sudkumar
Copy link
Author

sudkumar commented May 19, 2021

Thank you so much @zwu52 and the team. 👏

Considering the overwhelming on-going work on this project, this was quick. I honestly appreciate it.

And thank you for updates regarding rollout. Please release it as per your schedules. No hurries. ☺️

@sudkumar
Copy link
Author

🚀 . Got the fix in 8.6.2 . All working now.

Thank you @zwu52 and the team. 👏

@firebase firebase locked and limited conversation to collaborators Jun 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.