Skip to content

Commit

Permalink
fix auto resubscribe for macOS Safari
Browse files Browse the repository at this point in the history
Fixed by using existing Safari push token if available. Attempting to
prompt when permission is already granted still throws with user gesture
required error other wise.
  • Loading branch information
jkasten2 committed Oct 18, 2023
1 parent 31540b5 commit cb01847
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/shared/managers/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,23 +401,29 @@ export class SubscriptionManager {

const { deviceToken: existingDeviceToken } =
window.safari.pushNotification.permission(this.config.safariWebId);
if (!existingDeviceToken) {
/*
We're about to show the Safari native permission request. It can fail for a number of
reasons, e.g.:
- Setup-related reasons when developers just starting to get set up
- Address bar URL doesn't match safari certificate allowed origins (case-sensitive)
- Safari web ID doesn't match provided web ID
- Browsing in a Safari private window
- Bad icon DPI
but shouldn't fail for sites that have already gotten Safari working.
We'll show the permissionPromptDisplay event if the Safari user isn't already subscribed,
otherwise an already subscribed Safari user would not see the permission request again.
*/
OneSignalEvent.trigger(OneSignal.EVENTS.PERMISSION_PROMPT_DISPLAYED);

if (existingDeviceToken) {
pushSubscriptionDetails.setFromSafariSubscription(
existingDeviceToken.toLowerCase(),
);
return pushSubscriptionDetails;
}

/*
We're about to show the Safari native permission request. It can fail for a number of
reasons, e.g.:
- Setup-related reasons when developers just starting to get set up
- Address bar URL doesn't match safari certificate allowed origins (case-sensitive)
- Safari web ID doesn't match provided web ID
- Browsing in a Safari private window
- Bad icon DPI
but shouldn't fail for sites that have already gotten Safari working.
We'll show the permissionPromptDisplay event if the Safari user isn't already subscribed,
otherwise an already subscribed Safari user would not see the permission request again.
*/
OneSignalEvent.trigger(OneSignal.EVENTS.PERMISSION_PROMPT_DISPLAYED);
const deviceToken = await this.subscribeSafariPromptPermission();
PermissionUtils.triggerNotificationPermissionChanged();
if (deviceToken) {
Expand Down

0 comments on commit cb01847

Please sign in to comment.