firebase / firebase/firebase-js-sdk

messaging: allow calling useServiceWorker() multiple times

Open
#2,496 5 comments 0 reactions 0 assignees View on GitHub
api: messaging feature request
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

Quoting @gauntface in https://github.com/firebase/firebase-js-sdk/issues/114#issuecomment-351546545

> The error message does need changing. I hadn't intended that developers would call this method twice, out of interest what is the use case?

The use case is e.g. having a toggle to enable/disable push notifications.

> The reason for enforcing a single call is to avoid any risk of the library being called with two different service workers.

Ok, good thing! However, I know what I'm doing in this case, so I'd like to have a mechanism to circumvent the `registrationToUse != null` check.

> So would people prefer the ability to just allow any number of calls and block after getToken is called or would they prefer only being able to call it once and just have the error corrected to something along the lines of "useServiceWorker can only be called once and must be called before getToken()" ?

As I said, I need the possibility to call this method N times during the same app lifecycle...

Imagine this scenario:

- the app loads from scratch, the SW is registered... the user previously enabled push notifications, so now we call 1) useServiceWorker() 2) getToken() .... etc.
- user disable notifications from the app UI, we call deleteToken()... cool
- user wants to enable notifications again: here we do the same thing as when the app initialise, but we get this error...

The fix could be either to remove the check and not throw, to expose a setter for registrationToUse in order to be reset, or passing a new flag to useServiceWorker() in order to enable multiple calls.

Thanks! :pray:

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.