Skip to content

Commit

Permalink
use new URL in subscribeSafariPromptPermission
Browse files Browse the repository at this point in the history
Due to changes in our API, the `appId` is now required in the URL
used by `subscribeSafariPromptPermission`. In instances where the new
URL is not usable, we fallback to the legacy URL with an additional
request. This ensures compatibility with both the updated and legacy
API endpoints.
  • Loading branch information
fedterzi committed Nov 21, 2023
1 parent dcf7d0f commit 5e989b1
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/shared/managers/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type SubscriptionStateServiceWorkerNotIntalled =
export class SubscriptionManager {
private context: ContextSWInterface;
private config: SubscriptionManagerConfig;
private initialSafariPermissionPromptFailed = false;

constructor(context: ContextSWInterface, config: SubscriptionManagerConfig) {
this.context = context;
Expand Down Expand Up @@ -371,23 +372,40 @@ 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.initialSafariPermissionPromptFailed) {
const deviceToken = await requestPermission(
`${SdkEnvironment.getOneSignalApiUrl().toString()}/safari/apps/${
this.config.appId
}`,
);
if (!deviceToken) {
this.initialSafariPermissionPromptFailed = true;
}
return deviceToken;
} 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

0 comments on commit 5e989b1

Please sign in to comment.