-
Notifications
You must be signed in to change notification settings - Fork 886
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
messaging: allow calling useServiceWorker() multiple times #2496
Comments
I found a few problems with this issue:
|
I don't see why you'd need to call |
No, I'm not unregistering. I just have a toggle on the UI to enable/disable the push notifications. Why I have to call it more than once? Because if not, my logic would need some flag to discriminate if I called this function once, or more times... and I do not need any flag... I just need my logic to work the first, second, and Nth case, the same way, without having to introduce additional logic... Like it is possible to call My enablePush = async () => {
const permission = await Notification.requestPermission();
if (permission === 'granted') {
const registration = await navigator.serviceWorker.getRegistration();
// throws here when you enable for the second time
await messaging.useServiceWorker(registration);
const token = await this.messaging.getToken();
if (token != null) {
messaging.onTokenRefresh(myOnRefreshFn);
mySavePushTokenFn(token);
}
// ...
}
}; I hope that you can see that now... What I'm doing to avoid the error right now? I just added this line before the
This is an hack because that prop is marked as private in the source, it may not work in the future... I think a better way could be provided. What would you do? |
In case it would be useful, here is the disablePush = async () => {
theStopRefreshFnRetunedByInitRefresh();
const token = await messaging.getToken();
await messaging.deleteToken(token);
myDeletePushTokenFn(token);
}; |
For the record, here is the callback that executes when my SW registers (is called every time the app starts from scratch): const onSWRegister = (registration) => {
const hasPushSubscription = registration.pushManager
? await registration.pushManager.getSubscription()
: false;
if (hasPushSubscription) {
// cool, user has push enabled:
// basically the same logic as in the `enable` function:
// this never throws, because it could be only the first time
messaging.useServiceWorker(registration);
const token = await this.messaging.getToken();
if (token != null) {
messaging.onTokenRefresh(myOnRefreshFn);
mySavePushTokenFn(token);
}
}
}, |
Quoting @gauntface in #114 (comment)
The use case is e.g. having a toggle to enable/disable push notifications.
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.As I said, I need the possibility to call this method N times during the same app lifecycle...
Imagine this scenario:
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! 🙏
The text was updated successfully, but these errors were encountered: