diff --git a/src/managers/SubscriptionManager.ts b/src/managers/SubscriptionManager.ts index 6ebe25ed3..bb6421f37 100644 --- a/src/managers/SubscriptionManager.ts +++ b/src/managers/SubscriptionManager.ts @@ -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; @@ -258,23 +259,36 @@ export class SubscriptionManager { return !!deviceId; } - private subscribeSafariPromptPermission(): Promise { - return new Promise(resolve => { - window.safari.pushNotification.requestPermission( + private async subscribeSafariPromptPermission(): Promise { + const requestPermission = (url: string) => { + return new Promise((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 { @@ -308,6 +322,7 @@ export class SubscriptionManager { if (deviceToken) { pushSubscriptionDetails.setFromSafariSubscription(deviceToken); } else { + this.safariPermissionPromptFailed = true; throw new SubscriptionError(SubscriptionErrorReason.InvalidSafariSetup); } return pushSubscriptionDetails;