fix: webpush for iOS
This commit is contained in:
+31
@@ -22,6 +22,37 @@ export function pushSupported(): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
/** iPhone/iPad/iPod (incl. iPadOS, which reports as "Macintosh" + touch). */
|
||||
export function isIOS(): boolean {
|
||||
if (typeof navigator === "undefined") return false;
|
||||
const ua = navigator.userAgent;
|
||||
return (
|
||||
/iPad|iPhone|iPod/.test(ua) ||
|
||||
(navigator.maxTouchPoints > 1 && /Macintosh/.test(ua))
|
||||
);
|
||||
}
|
||||
|
||||
/** Running as an installed (Home Screen) app, not a browser tab. */
|
||||
export function isStandalone(): boolean {
|
||||
if (typeof window === "undefined") return false;
|
||||
return (
|
||||
// iOS Safari's non-standard flag…
|
||||
(navigator as Navigator & { standalone?: boolean }).standalone === true ||
|
||||
// …and the standard display-mode for everyone else.
|
||||
window.matchMedia("(display-mode: standalone)").matches
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* iOS only allows Web Push when the site has been added to the Home Screen
|
||||
* (iOS 16.4+). In a plain Safari tab the push APIs don't exist, so this is
|
||||
* the case where we should guide the user to "Add to Home Screen" rather
|
||||
* than show a dead "unsupported".
|
||||
*/
|
||||
export function needsIOSInstall(): boolean {
|
||||
return isIOS() && !isStandalone() && !pushSupported();
|
||||
}
|
||||
|
||||
/** The browser's current notification permission, or "unsupported". */
|
||||
export function pushPermission(): NotificationPermission | "unsupported" {
|
||||
if (!pushSupported()) return "unsupported";
|
||||
|
||||
Reference in New Issue
Block a user