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

Fix prompting again after closing notification permission prompt if site has its own ServiceWorker #1192

Merged
merged 3 commits into from
Sep 11, 2024

Conversation

jkasten2
Copy link
Member

@jkasten2 jkasten2 commented Sep 11, 2024

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:

  1. requestNotificationPermission() - this was always returning undefined due to a conversion bug
  2. getRegistration() - did not check if the service worker was a OneSignal one, resulting in wrong assumptions being made.

Validation

Tests

Added a unit test for requestNotificationPermission(), this was always returning undefined before the changes in the first commit.

Checklist

  • All the automated tests pass or I explained why that is not possible
  • I have personally tested this on my machine or explained why that is not possible
  • I have included test coverage for these changes or explained why they are not needed

Programming Checklist
Interfaces:

  • Don't use default export
  • New interfaces are in model files

Functions:

  • Don't use default export
  • All function signatures have return types
  • Helpers should not access any data but rather be given the data to operate on.

Typescript:

  • No Typescript warnings
  • Avoid silencing null/undefined warnings with the exclamation point

Other:

  • Iteration: refrain from using elem of array syntax. Prefer forEach or use map
  • Avoid using global OneSignal accessor for context if possible. Instead, we can pass it to function/constructor so that we don't call OneSignal.context

Screenshots

Info

Checklist

  • I have included screenshots/recordings of the intended results or explained why they are not needed

Related Tickets



This change is Reviewable

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.
@jkasten2 jkasten2 force-pushed the fix/double-prompting-with-sw branch from ffdd946 to f3515f7 Compare September 11, 2024 00:17
Copy link
Contributor

@shepherd-l shepherd-l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

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.

  1. Clear web cache
  2. Open website
  3. Click "Subscribe" on slide down push prompt
  4. 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.

Copy link
Member Author

@jkasten2 jkasten2 left a 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();

Sandbox to try it out:
https://www.typescriptlang.org/play/?#code/IYZwngdgxgBAZgV2gFwJYHsI1JKAlAUxAQBtkAKASgC4YAFAJ3QFtUQCAeAOQWYCMCDAHwwA3gCgYMBgWQIGWRizYEAdDJDoSANwLkAjACYAzJQDc4gL7jxAelv0ZcQTIAmMcs3QyYUTFBVKGABaGAAVAAs2GGZZCPR3V3QiCAByZF9MYliYZAiCGG1gEgQCABoYTRgAdwKktIyIAgJ3VOBq4FRkVNVxHGh4JCg0TBgIdABBDq6AUQAPYGYABxI9Gkdldm5eAWExSWlZeSx+-CJSCnMrG3sYAFUIJqgiEGAGMA8vH10GPnR2IKhACa6AQvmAWCgxRIMDa026MD4CAyeUEBTYDWwMCW6FQEAyqDguXygxQGCwSRS6TGzXcyHQh2AdJJRRK5Rg3hqnQyo28rkEHKJ6CWgmAIwgID64AGiDJo3a3PmixWa1oSlYWx4-EEIgkUhkcgU2Hh2GlZ2IZCoFmsdgcAGEEgUQPFqniAOaI9B5GreADWxKdi3Z42CIFQ-JgBDgzmGIF6p1Jw3J2IYeOQhAtyBAVDVTA1nG0uNcuoOfglWjUJHQbvIACIFbMFstVrRazAANTG7ldxvK1ZUK5SMuaVaqKs12vjKaKpsq1sdnuNSbwpXNtZXaxLVP4jMXbNXIA


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 and registerForPushNotifications: after OneSignal.initialized even though they still passed before, and now log PushPermissionNotGrantedError: 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.

@jkasten2 jkasten2 merged commit 1f64c20 into main Sep 11, 2024
4 of 5 checks passed
@jkasten2 jkasten2 deleted the fix/double-prompting-with-sw branch September 11, 2024 20:33
@shepherd-l shepherd-l mentioned this pull request Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants