Skip to content

Email for professionals and teams

add browser notification

Hieu Nguyen (Jack)f4e3c31512e0
diff --git a/src/app/(settings)/settings/inbox/page.tsx b/src/app/(settings)/settings/inbox/page.tsx
index 097200f..1c6f1a5 100644
--- a/src/app/(settings)/settings/inbox/page.tsx
+++ b/src/app/(settings)/settings/inbox/page.tsx
@@ -2,10 +2,20 @@ import { InboxThreadingSettings } from "@/components/settings/inbox-threading-se
import { InboxShortcutsSettings } from "@/components/settings/inbox-shortcuts-settings";
import { MailboxAutoReplyForm } from "@/components/settings/mailbox-auto-reply-form";
import { SpamFilterSettings } from "@/components/settings/spam-filter-settings";
+import { BrowserNotificationSettings } from "@/components/settings/browser-notification-settings";
export default function SettingsInboxPage() {
return (
<div className="space-y-8 py-4">
+ <section className="space-y-4">
+ <div>
+ <h2 className="text-xl font-semibold text-neutral-900">Notifications</h2>
+ <p className="mt-1 text-sm text-neutral-500">Choose how you hear about new email.</p>
+ </div>
+ <div className="rounded-3xl bg-white p-6">
+ <BrowserNotificationSettings />
+ </div>
+ </section>
<section className="space-y-4">
<div>
<h2 className="text-xl font-semibold text-neutral-900">Spam protection</h2>
diff --git a/src/components/settings/browser-notification-settings.tsx b/src/components/settings/browser-notification-settings.tsx
new file mode 100644
index 0000000..2293e91
--- /dev/null
+++ b/src/components/settings/browser-notification-settings.tsx
@@ -0,0 +1,76 @@
+"use client";
+
+import { useEffect, useState } from "react";
+import { Switch } from "@/components/ui/switch";
+import {
+ areBrowserNotificationsEnabled,
+ setBrowserNotificationsEnabled,
+} from "@/hooks/message-realtime-utils";
+
+export function BrowserNotificationSettings() {
+ const [permission, setPermission] = useState<NotificationPermission | "unsupported">("unsupported");
+ const [enabled, setEnabled] = useState(false);
+ const [error, setError] = useState<string | null>(null);
+
+ useEffect(() => {
+ const refresh = () => {
+ setPermission(typeof Notification === "undefined" ? "unsupported" : Notification.permission);
+ setEnabled(areBrowserNotificationsEnabled());
+ };
+ refresh();
+ window.addEventListener("focus", refresh);
+ window.addEventListener("storage", refresh);
+ return () => {
+ window.removeEventListener("focus", refresh);
+ window.removeEventListener("storage", refresh);
+ };
+ }, []);
+
+ return (
+ <div>
+ <label className="flex items-start gap-3 rounded-xl bg-neutral-50 p-4">
+ <span className="flex-1">
+ <span className="block text-sm font-medium text-neutral-900">Browser notifications</span>
+ <span className="mt-1 block text-sm text-neutral-500">
+ Show a notification for new email while Mailflare is open in a background tab.
+ </span>
+ </span>
+ <Switch
+ checked={permission === "granted" && enabled}
+ disabled={permission === "unsupported" || permission === "denied"}
+ onCheckedChange={(nextEnabled) => {
+ setError(null);
+ if (!nextEnabled) {
+ try {
+ setBrowserNotificationsEnabled(false);
+ setEnabled(false);
+ } catch {
+ setError("Could not save the notification preference in this browser.");
+ }
+ return;
+ }
+ void (async () => {
+ try {
+ const nextPermission = permission === "granted" ? permission : await Notification.requestPermission();
+ setPermission(nextPermission);
+ if (nextPermission !== "granted") return;
+ setBrowserNotificationsEnabled(true);
+ setEnabled(true);
+ } catch {
+ setError("Could not enable browser notifications.");
+ }
+ })();
+ }}
+ aria-label="Enable browser notifications"
+ />
+ </label>
+ {permission === "denied" && (
+ <p className="mt-2 px-4 text-sm text-neutral-500">Notifications are blocked. Allow them in your browser&apos;s site settings to enable this option.</p>
+ )}
+ {permission === "unsupported" && (
+ <p className="mt-2 px-4 text-sm text-neutral-500">Browser notifications are unavailable here.</p>
+ )}
+ {error && <p className="mt-2 px-4 text-sm text-red-600">{error}</p>}
+ </div>
+ );
+}
diff --git a/src/hooks/message-realtime-utils.ts b/src/hooks/message-realtime-utils.ts
index 4c774ab..93178b9 100644
--- a/src/hooks/message-realtime-utils.ts
+++ b/src/hooks/message-realtime-utils.ts
@@ -3,6 +3,19 @@ import type { NewMessageEvent } from "./message-realtime-types";
export const REALTIME_FALLBACK_INTERVAL_MS = 60_000;
export const REALTIME_HEARTBEAT_INTERVAL_MS = 25_000;
export const REALTIME_RECONNECT_MAX_MS = 30_000;
+const BROWSER_NOTIFICATIONS_ENABLED_KEY = "mailflare:browser-notifications-enabled";
+
+export function areBrowserNotificationsEnabled(): boolean {
+ try {
+ return window.localStorage.getItem(BROWSER_NOTIFICATIONS_ENABLED_KEY) !== "false";
+ } catch {
+ return true;
+ }
+}
+
+export function setBrowserNotificationsEnabled(enabled: boolean): void {
+ window.localStorage.setItem(BROWSER_NOTIFICATIONS_ENABLED_KEY, String(enabled));
+}
export function getRealtimeWebSocketUrl(): string {
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
@@ -42,19 +55,24 @@ export function showBrowserNewMessageNotification(event: NewMessageEvent): void
if (
typeof Notification === "undefined" ||
Notification.permission !== "granted" ||
+ !areBrowserNotificationsEnabled() ||
document.visibilityState === "visible"
) {
return;
}
- const notification = new Notification(event.subject || "New email", {
- body: `From ${event.fromName ?? event.from}`,
- icon: "/icon-96.png",
- tag: event.messageId,
- });
- notification.onclick = () => {
- window.focus();
- window.location.assign(`/inbox/${event.messageId}`);
- notification.close();
- };
+ try {
+ const notification = new Notification(event.subject || "New email", {
+ body: `From ${event.fromName ?? event.from}`,
+ icon: "/icon-96.png",
+ tag: event.messageId,
+ });
+ notification.onclick = () => {
+ window.focus();
+ window.location.assign(`/inbox/${event.messageId}`);
+ notification.close();
+ };
+ } catch {
+ // The in-app new message popup still handles this email.
+ }
}