Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

player-model/use new URL in subscribeSafariPromptPermission #1143

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading