fix: iOS push notification prompt
iOS requires user gesture for Notification.requestPermission(). - Added NotificationPrompt banner that shows on permission='default' - autoSubscribePush only fires when already 'granted' (re-subscribe on restore) - Banner uses tap handler for permission request (works on iOS) - Gruvbox themed, dismissible, bottom-center toast
This commit is contained in:
@@ -8,6 +8,7 @@ import { useLayoutStore } from '../stores/layout.ts';
|
|||||||
import { ServerBar } from './ServerBar.tsx';
|
import { ServerBar } from './ServerBar.tsx';
|
||||||
import { ChannelList } from './ChannelList.tsx';
|
import { ChannelList } from './ChannelList.tsx';
|
||||||
import { ConversationList } from './ConversationList.tsx';
|
import { ConversationList } from './ConversationList.tsx';
|
||||||
|
import { NotificationPrompt } from './NotificationPrompt.tsx';
|
||||||
import { MemberList } from './MemberList.tsx';
|
import { MemberList } from './MemberList.tsx';
|
||||||
import { VoicePanel } from './VoicePanel.tsx';
|
import { VoicePanel } from './VoicePanel.tsx';
|
||||||
import { ServerSettingsModal } from './ServerSettingsModal.tsx';
|
import { ServerSettingsModal } from './ServerSettingsModal.tsx';
|
||||||
@@ -223,6 +224,7 @@ export function Layout() {
|
|||||||
<span>{new Date().toISOString().slice(0, 10)}</span>
|
<span>{new Date().toISOString().slice(0, 10)}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<NotificationPrompt />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { usePushStore } from '../stores/push.ts';
|
||||||
|
|
||||||
|
export function NotificationPrompt() {
|
||||||
|
const { isSupported, permission, isSubscribed, subscribe } = usePushStore();
|
||||||
|
const [dismissed, setDismissed] = useState(false);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (localStorage.getItem('push-prompt-dismissed') === 'true') {
|
||||||
|
setDismissed(true);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Don't show if not supported, already subscribed, permission resolved, or dismissed
|
||||||
|
if (!isSupported || isSubscribed || permission === 'granted' || permission === 'denied' || dismissed) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleEnable = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
await subscribe();
|
||||||
|
} catch {
|
||||||
|
// user denied or error
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDismiss = () => {
|
||||||
|
setDismissed(true);
|
||||||
|
localStorage.setItem('push-prompt-dismissed', 'true');
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed bottom-4 left-1/2 -translate-x-1/2 z-50 max-w-sm w-[calc(100%-2rem)] bg-gb-bg-s border border-gb-bg-t rounded-lg shadow-2xl p-4 flex items-center gap-3">
|
||||||
|
<div className="text-2xl">🔔</div>
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<p className="text-sm font-medium text-gb-fg">Enable notifications?</p>
|
||||||
|
<p className="text-xs text-gb-fg-f">Get notified about new messages</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2 shrink-0">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleDismiss}
|
||||||
|
className="text-xs px-3 py-1.5 rounded-lg text-gb-fg-f hover:text-gb-fg transition-colors"
|
||||||
|
>
|
||||||
|
Not now
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleEnable}
|
||||||
|
disabled={loading}
|
||||||
|
className="text-xs px-3 py-1.5 rounded-lg bg-gb-aqua text-gb-bg font-bold hover:opacity-90 transition-opacity disabled:opacity-50"
|
||||||
|
>
|
||||||
|
{loading ? '...' : 'Enable'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -72,10 +72,12 @@ interface AuthState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ponytail: auto-subscribe to push notifications on auth
|
// ponytail: auto-subscribe to push notifications on auth
|
||||||
|
// Only fires when permission is already 'granted' (re-subscribe on session restore).
|
||||||
|
// iOS requires a user gesture for the initial permission prompt — that's handled by NotificationPrompt.
|
||||||
function autoSubscribePush() {
|
function autoSubscribePush() {
|
||||||
import("../stores/push.ts").then(({ usePushStore }) => {
|
import("../stores/push.ts").then(({ usePushStore }) => {
|
||||||
const ps = usePushStore.getState();
|
const ps = usePushStore.getState();
|
||||||
if (ps.isSupported && Notification.permission !== 'denied' && !ps.isSubscribed) {
|
if (ps.isSupported && Notification.permission === 'granted' && !ps.isSubscribed) {
|
||||||
ps.subscribe();
|
ps.subscribe();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user