Skip to content

Commit

Permalink
Merge pull request #1135 from OneSignal/feat/show_ios_supported_message
Browse files Browse the repository at this point in the history
[Feat] Show iOS supported message
  • Loading branch information
jkasten2 authored Nov 17, 2023
2 parents dcf7d0f + 9fb9870 commit fd46f19
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/page/utils/BrowserSupportsPush.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Light weight JS to detect browsers push notification capabilities
//
// This is used by the OneSignalSDK.page.js shim
// DO NOT add other imports since it is an ES5 target and dead code imports can't be clean up.

Expand All @@ -7,15 +9,17 @@ export function isPushNotificationsSupported() {
return supportsVapidPush() || supportsSafariPush();
}

// Fallback detection for Safari on macOS in an iframe context
// - window.safari is undefined in this context
export function isMacOSSafariInIframe(): boolean {
// Fallback detection for Safari on macOS in an iframe context
return (
window.top !== window && // isContextIframe
navigator.vendor === 'Apple Computer, Inc.' && // isSafari
isSafariBrowser() &&
navigator.platform === 'MacIntel'
); // isMacOS
}

// Does the browser support legacy Safari push? (only available on macOS)
export function supportsSafariPush(): boolean {
return (
(window.safari && typeof window.safari.pushNotification !== 'undefined') ||
Expand All @@ -32,6 +36,19 @@ export function supportsVapidPush(): boolean {
);
}

// Is Safari on iOS or iPadOS
export function isIosSafari(): boolean {
// Safari's "Request Desktop Website" (default for iPad) masks the
// userAgent as macOS. So we are using maxTouchPoints to assume it is
// iOS, since there are no touch screen Macs.
return isSafariBrowser() && navigator.maxTouchPoints > 0;
}

// Is any Safari browser, includes macOS and iOS.
function isSafariBrowser(): boolean {
return navigator.vendor === 'Apple Computer, Inc.';
}

/* Notes on browser results which lead the logic of the functions above */
// Safari
// macOS - typeof safari.pushNotification == "object"
Expand All @@ -43,4 +60,3 @@ export function supportsVapidPush(): boolean {
// Firefox
// Normal - typeof PushSubscriptionOptions == "function"
// HTTP & HTTPS - typeof PushSubscriptionOptions == "function"
// ESR - typeof PushSubscriptionOptions == "undefined"
16 changes: 14 additions & 2 deletions src/page/utils/OneSignalShimLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { isPushNotificationsSupported } from './BrowserSupportsPush';
import {
isIosSafari,
isPushNotificationsSupported,
} from './BrowserSupportsPush';
// NOTE: Careful if adding imports, ES5 targets can't clean up functions never called.

// See sdk.ts for what entry points this handles
Expand Down Expand Up @@ -55,7 +58,16 @@ export class OneSignalShimLoader {
if (isPushNotificationsSupported()) {
OneSignalShimLoader.loadFullPageSDK();
} else {
console.log('OneSignal: SDK is not compatible with this browser.');
this.printEnvironmentNotSupported();
}
}

private static printEnvironmentNotSupported() {
let logMessage = 'OneSignal: SDK is not compatible with this browser.';
if (isIosSafari()) {
logMessage +=
' To support iOS please install as a Web App. See the OneSignal guide https://documentation.onesignal.com/docs/safari-web-push-for-ios';
}
console.log(logMessage);
}
}

0 comments on commit fd46f19

Please sign in to comment.