-
Notifications
You must be signed in to change notification settings - Fork 1
feat: ✨ ios notifications #554
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
Open
ethancha0
wants to merge
8
commits into
main
Choose a base branch
from
ewc-ios-notifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b23027e
feat: ✨ ios notifications
ethancha0 2e80cf1
feat: ✨ push notif setting
ethancha0 cad10ba
fix: 🐛 cubic
ethancha0 96b6de5
fix: 🐛 rm web push notif system
ethancha0 e945943
fix: 🐛 cleanup
ethancha0 5ac9f60
fix: 🐛 edge cases
ethancha0 7e546d3
feat: ✨ switch firebase to aws sns
ethancha0 567061f
chore: 🔧 rm dead firebase code
ethancha0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect } from "react"; | ||
| import { isNativeIosApp } from "@/lib/platform"; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| webkit?: { | ||
| messageHandlers?: Record< | ||
| string, | ||
| { | ||
| postMessage: (message: string) => void; | ||
| } | ||
| >; | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function getBridgeHandler(name: string) { | ||
| return window.webkit?.messageHandlers?.[name]; | ||
| } | ||
|
|
||
| function getUserTopic(userId: string) { | ||
| return `user_${userId}`; | ||
| } | ||
|
|
||
| function postMessage(name: string, payload?: unknown) { | ||
| const handler = getBridgeHandler(name); | ||
| if (!handler) return; | ||
| handler.postMessage(payload ? JSON.stringify(payload) : ""); | ||
| } | ||
|
|
||
| type NativeIosPushBridgeProps = { | ||
| userId: string; | ||
| }; | ||
|
|
||
| export function NativeIosPushBridge({ userId }: NativeIosPushBridgeProps) { | ||
| useEffect(() => { | ||
| if (!isNativeIosApp()) return; | ||
| if (!window.webkit?.messageHandlers) return; | ||
|
|
||
| const topic = getUserTopic(userId); | ||
| const subscribe = () => postMessage("push-subscribe", { topic }); | ||
|
|
||
| const handlePermissionState = (event: Event) => { | ||
| const detail = (event as CustomEvent<string>).detail; | ||
| if ( | ||
| detail === "authorized" || | ||
| detail === "ephemeral" || | ||
| detail === "provisional" | ||
| ) { | ||
| subscribe(); | ||
| postMessage("push-token"); | ||
| return; | ||
| } | ||
|
|
||
| if (detail === "notDetermined") { | ||
| postMessage("push-permission-request"); | ||
| } | ||
| }; | ||
|
|
||
| const handlePermissionRequestResult = (event: Event) => { | ||
| const detail = (event as CustomEvent<string>).detail; | ||
| if (detail === "granted") { | ||
| subscribe(); | ||
| postMessage("push-token"); | ||
| } | ||
| }; | ||
|
|
||
| window.addEventListener("push-permission-state", handlePermissionState); | ||
| window.addEventListener( | ||
| "push-permission-request", | ||
| handlePermissionRequestResult, | ||
| ); | ||
|
|
||
| postMessage("push-permission-state"); | ||
|
|
||
| return () => { | ||
| window.removeEventListener( | ||
| "push-permission-state", | ||
| handlePermissionState, | ||
| ); | ||
| window.removeEventListener( | ||
| "push-permission-request", | ||
| handlePermissionRequestResult, | ||
| ); | ||
| postMessage("push-subscribe", { topic, unsubscribe: true }); | ||
| }; | ||
| }, [userId]); | ||
|
|
||
| return null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| "use client"; | ||
|
|
||
| import NotificationsActiveOutlinedIcon from "@mui/icons-material/NotificationsActiveOutlined"; | ||
| import Button from "@mui/material/Button"; | ||
| import Stack from "@mui/material/Stack"; | ||
| import Typography from "@mui/material/Typography"; | ||
| import { useEffect, useState } from "react"; | ||
| import { useSnackbar } from "@/components/ui/snackbar-provider"; | ||
| import { isNativeIosApp } from "@/lib/platform"; | ||
|
|
||
| type PermissionState = NotificationPermission | "unsupported"; | ||
|
|
||
| function browserSupportsNotifications() { | ||
| return ( | ||
| typeof window !== "undefined" && | ||
| "Notification" in window && | ||
| "serviceWorker" in navigator | ||
| ); | ||
| } | ||
|
|
||
| function isStandaloneWebApp() { | ||
| if (typeof window === "undefined") return false; | ||
|
|
||
| return ( | ||
| window.matchMedia("(display-mode: standalone)").matches || | ||
| ("standalone" in navigator && | ||
| (navigator as Navigator & { standalone?: boolean }).standalone === true) | ||
| ); | ||
| } | ||
|
|
||
| function getInitialPermission(): PermissionState { | ||
| if (!browserSupportsNotifications()) return "unsupported"; | ||
| return Notification.permission; | ||
| } | ||
|
|
||
| export function WebPushPermissionButton() { | ||
| const [permission, setPermission] = | ||
| useState<PermissionState>(getInitialPermission); | ||
| const [isStandalone, setIsStandalone] = useState(false); | ||
| const [isNative, setIsNative] = useState(false); | ||
| const [isRequesting, setIsRequesting] = useState(false); | ||
| const { showSuccess, showError } = useSnackbar(); | ||
|
|
||
| useEffect(() => { | ||
| setPermission(getInitialPermission()); | ||
| setIsStandalone(isStandaloneWebApp()); | ||
| setIsNative(isNativeIosApp()); | ||
| }, []); | ||
|
|
||
| if (isNative || permission === "unsupported") return null; | ||
|
|
||
| const handleEnable = async () => { | ||
| if (!browserSupportsNotifications()) return; | ||
|
|
||
| setIsRequesting(true); | ||
| try { | ||
| await navigator.serviceWorker.ready; | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
|
||
| const nextPermission = await Notification.requestPermission(); | ||
| setPermission(nextPermission); | ||
|
|
||
| if (nextPermission === "granted") { | ||
| showSuccess("Notification permission enabled"); | ||
| } else { | ||
| showError("Notification permission was not enabled"); | ||
| } | ||
| } catch { | ||
| showError("Failed to request notification permission"); | ||
| } finally { | ||
| setIsRequesting(false); | ||
| } | ||
| }; | ||
|
|
||
| const statusText = | ||
| permission === "granted" | ||
| ? "Browser notification permission is enabled on this device." | ||
| : permission === "denied" | ||
| ? "Browser notification permission is blocked in system settings." | ||
| : isStandalone | ||
| ? "Enable browser notification permission for this home-screen app." | ||
| : "Install ZotMeet to your home screen to enable iOS browser notifications."; | ||
|
|
||
| return ( | ||
| <Stack | ||
| direction={{ xs: "column", sm: "row" }} | ||
| spacing={2} | ||
| alignItems={{ xs: "flex-start", sm: "center" }} | ||
| justifyContent="space-between" | ||
| sx={{ | ||
| py: 2, | ||
| borderBottom: 1, | ||
| borderColor: "divider", | ||
| }} | ||
| > | ||
| <Stack spacing={0.75}> | ||
| <Typography variant="body1" fontWeight={500}> | ||
| Device Notifications | ||
| </Typography> | ||
| <Typography variant="body2" color="text.secondary"> | ||
| {statusText} | ||
| </Typography> | ||
| </Stack> | ||
| <Button | ||
| variant="outlined" | ||
| startIcon={<NotificationsActiveOutlinedIcon />} | ||
| onClick={handleEnable} | ||
| disabled={permission !== "default" || !isStandalone || isRequesting} | ||
| sx={{ flexShrink: 0 }} | ||
| > | ||
| {permission === "granted" | ||
| ? "Enabled" | ||
| : permission === "denied" | ||
| ? "Blocked" | ||
| : isRequesting | ||
| ? "Enabling..." | ||
| : "Enable"} | ||
| </Button> | ||
| </Stack> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.