fix: webpush for iOS

This commit is contained in:
gosangam
2026-06-08 16:09:09 +05:30
parent 0334e03c3c
commit 90df5bc784
3 changed files with 51 additions and 2 deletions
+31
View File
@@ -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";