Skip to content

Commit 7b3686f

Browse files
authored
Merge pull request #1143 from OneSignal/player-model/use_new_safari_subscription_url
player-model/use new URL in `subscribeSafariPromptPermission`
2 parents e6caaff + 5e056b9 commit 7b3686f

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/managers/SubscriptionManager.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export type SubscriptionStateServiceWorkerNotIntalled =
5151
export class SubscriptionManager {
5252
private context: ContextSWInterface;
5353
private config: SubscriptionManagerConfig;
54+
private safariPermissionPromptFailed = false;
5455

5556
constructor(context: ContextSWInterface, config: SubscriptionManagerConfig) {
5657
this.context = context;
@@ -258,23 +259,36 @@ export class SubscriptionManager {
258259
return !!deviceId;
259260
}
260261

261-
private subscribeSafariPromptPermission(): Promise<string | null> {
262-
return new Promise<string>(resolve => {
263-
window.safari.pushNotification.requestPermission(
262+
private async subscribeSafariPromptPermission(): Promise<string | null> {
263+
const requestPermission = (url: string) => {
264+
return new Promise<string | null>((resolve) => {
265+
window.safari.pushNotification.requestPermission(
266+
url,
267+
this.config.safariWebId,
268+
{ app_id: this.config.appId },
269+
(response) => {
270+
if (response && response.deviceToken) {
271+
resolve(response.deviceToken.toLowerCase());
272+
} else {
273+
resolve(null);
274+
}
275+
},
276+
);
277+
});
278+
};
279+
280+
if (!this.safariPermissionPromptFailed) {
281+
return requestPermission(
282+
`${SdkEnvironment.getOneSignalApiUrl().toString()}/safari/apps/${
283+
this.config.appId
284+
}`,
285+
);
286+
} else {
287+
// If last attempt failed, retry with the legacy URL
288+
return requestPermission(
264289
`${SdkEnvironment.getOneSignalApiUrl().toString()}/safari`,
265-
this.config.safariWebId,
266-
{
267-
app_id: this.config.appId
268-
},
269-
response => {
270-
if ((response as any).deviceToken) {
271-
resolve((response as any).deviceToken.toLowerCase());
272-
} else {
273-
resolve(null);
274-
}
275-
}
276290
);
277-
});
291+
}
278292
}
279293

280294
private async subscribeSafari(): Promise<RawPushSubscription> {
@@ -308,6 +322,7 @@ export class SubscriptionManager {
308322
if (deviceToken) {
309323
pushSubscriptionDetails.setFromSafariSubscription(deviceToken);
310324
} else {
325+
this.safariPermissionPromptFailed = true;
311326
throw new SubscriptionError(SubscriptionErrorReason.InvalidSafariSetup);
312327
}
313328
return pushSubscriptionDetails;

0 commit comments

Comments
 (0)