-
Notifications
You must be signed in to change notification settings - Fork 116
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
Fix prompting again after closing notification permission prompt if site has its own ServiceWorker #1192
Conversation
requestNotificationPermission() was returning undefined due to incorrectly converting a string to an enum.
Created a new getOneSignalRegistration() function to guarantee the service worker is OneSignal's. This encapsulates this so consuming code doesn't have to account for this, and it was found out most code was not.
Remove this unused state that was only for the v15 SDK.
ffdd946
to
f3515f7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can confirm that the native permission prompt does not show again after dismissing with "X" with this fix. Tested with the example web app using a using a dummy non-OneSignal service worker.
Bug reproduced by:
Using the example web app using a dummy non-OneSignal service worker.
- Clear web cache
- Open website
- Click "Subscribe" on slide down push prompt
- Dismiss native permission prompt with "X"
- Main: Shows native permission prompt again
- This PR: Does not show native permission prompt again
Reviewable status: 0 of 7 files reviewed, 2 unresolved discussions (waiting on @emawby, @jennantilla, @jinliu9508, @jkasten2, @nan-li, and @rgomezp)
src/shared/managers/ServiceWorkerManager.ts
line 42 at r3 (raw file):
ServiceWorkerRegistration | null | undefined > { return await ServiceWorkerUtilHelper.getRegistration(
Why was this await removed? Isn't getRegistration()
an async method that should be awaited?
src/shared/managers/SubscriptionManager.ts
line 336 at r1 (raw file):
// TODO: Clean up our custom NotificationPermission enum // in favor of TS union type NotificationPermission instead of converting return results as NotificationPermission;
Nice find! I noticed that this fixes Error: OneSignal service worker not found!
from the tests: registerForPushNotifications: before OneSignal.initialized
and registerForPushNotifications: after OneSignal.initialized
even though they still passed before, and now log PushPermissionNotGrantedError: The user dismissed the permission prompt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for testing!
Reviewable status: 0 of 7 files reviewed, 2 unresolved discussions (waiting on @emawby, @jennantilla, @jinliu9508, @nan-li, @rgomezp, and @shepherd-l)
src/shared/managers/ServiceWorkerManager.ts
line 42 at r3 (raw file):
Previously, shepherd-l (Shepherd) wrote…
Why was this await removed? Isn't
getRegistration()
an async method that should be awaited?
The function doesn't read the value, nor does it do anything after which would affect any order of operations.
Here is an example I put together to show there is no-side effects here:
async function asyncResult(): Promise<Number> {
return Promise.resolve(123);
}
// Preferred (more concise) - This method doesn't consume the value, so we don't need 'await'.
async function noAwaitExample(): Promise<Number> {
return asyncResult();
}
// Unnecessary (more verbose) - You can call 'await' but there isn't a point if the function doesn't need to read the value, or wait on order of operations
async function awaitExample(): Promise<Number> {
return await asyncResult();
}
// Code showing both work the same, no-side effects.
async function printResults(): Promise<void> {
console.log("awaitExample: " + await awaitExample());
console.log("noAwaitExample: " + await noAwaitExample());
}
printResults();
src/shared/managers/SubscriptionManager.ts
line 336 at r1 (raw file):
Previously, shepherd-l (Shepherd) wrote…
Nice find! I noticed that this fixes
Error: OneSignal service worker not found!
from the tests:registerForPushNotifications: before OneSignal.initialized
andregisterForPushNotifications: after OneSignal.initialized
even though they still passed before, and now logPushPermissionNotGrantedError: The user dismissed the permission prompt.
ya, this is why I created a new unit test file to make sure the conversion is done correctly.
Description
1 Line Summary
Fix prompting again after closing notification permission prompt if site has its own ServiceWorker.
Details
This PR fixes two things related to this issue:
undefined
due to a conversion bugValidation
Tests
Added a unit test for
requestNotificationPermission()
, this was always returningundefined
before the changes in the first commit.Checklist
Programming Checklist
Interfaces:
Functions:
Typescript:
Other:
elem of array
syntax. PreferforEach
or usemap
context
if possible. Instead, we can pass it to function/constructor so that we don't callOneSignal.context
Screenshots
Info
Checklist
Related Tickets
This change is