Skip to content

Commit

Permalink
Merge pull request #1143 from OneSignal/player-model/use_new_safari_s…
Browse files Browse the repository at this point in the history
…ubscription_url

player-model/use new URL in `subscribeSafariPromptPermission`
  • Loading branch information
jkasten2 authored Nov 27, 2023
2 parents e6caaff + 5e056b9 commit 7b3686f
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/managers/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type SubscriptionStateServiceWorkerNotIntalled =
export class SubscriptionManager {
private context: ContextSWInterface;
private config: SubscriptionManagerConfig;
private safariPermissionPromptFailed = false;

constructor(context: ContextSWInterface, config: SubscriptionManagerConfig) {
this.context = context;
Expand Down Expand Up @@ -258,23 +259,36 @@ export class SubscriptionManager {
return !!deviceId;
}

private subscribeSafariPromptPermission(): Promise<string | null> {
return new Promise<string>(resolve => {
window.safari.pushNotification.requestPermission(
private async subscribeSafariPromptPermission(): Promise<string | null> {
const requestPermission = (url: string) => {
return new Promise<string | null>((resolve) => {
window.safari.pushNotification.requestPermission(
url,
this.config.safariWebId,
{ app_id: this.config.appId },
(response) => {
if (response && response.deviceToken) {
resolve(response.deviceToken.toLowerCase());
} else {
resolve(null);
}
},
);
});
};

if (!this.safariPermissionPromptFailed) {
return requestPermission(
`${SdkEnvironment.getOneSignalApiUrl().toString()}/safari/apps/${
this.config.appId
}`,
);
} else {
// If last attempt failed, retry with the legacy URL
return requestPermission(
`${SdkEnvironment.getOneSignalApiUrl().toString()}/safari`,
this.config.safariWebId,
{
app_id: this.config.appId
},
response => {
if ((response as any).deviceToken) {
resolve((response as any).deviceToken.toLowerCase());
} else {
resolve(null);
}
}
);
});
}
}

private async subscribeSafari(): Promise<RawPushSubscription> {
Expand Down Expand Up @@ -308,6 +322,7 @@ export class SubscriptionManager {
if (deviceToken) {
pushSubscriptionDetails.setFromSafariSubscription(deviceToken);
} else {
this.safariPermissionPromptFailed = true;
throw new SubscriptionError(SubscriptionErrorReason.InvalidSafariSetup);
}
return pushSubscriptionDetails;
Expand Down

0 comments on commit 7b3686f

Please sign in to comment.