mirror of
https://github.com/fromchat-messenger/web.git
synced 2026-09-23 11:35:07 +03:00
Implement notifications for Electron
This commit is contained in:
@@ -2,7 +2,8 @@ import { useState, useEffect } from "react";
|
||||
import { PRODUCT_NAME, API_BASE_URL } from "../../../core/config";
|
||||
import type { DialogProps } from "../../../core/types";
|
||||
import { MaterialDialog } from "../core/Dialog";
|
||||
import { pushNotificationManager } from "../../../utils/pushNotifications";
|
||||
import { initialize, isSupported, startElectronReceiver, stopElectronReceiver, subscribe, unsubscribe } from "../../../utils/notifications";
|
||||
import { isElectron } from "../../../electron/electron";
|
||||
import { useAppState } from "../../state";
|
||||
import type { Switch } from "mdui/components/switch";
|
||||
import { getAuthHeaders } from "../../../auth/api";
|
||||
@@ -14,8 +15,10 @@ export function SettingsDialog({ isOpen, onOpenChange }: DialogProps) {
|
||||
const user = useAppState(state => state.user);
|
||||
|
||||
useEffect(() => {
|
||||
setPushSupported(pushNotificationManager.isSupported());
|
||||
setPushNotificationsEnabled(!!pushNotificationManager.getSubscription());
|
||||
setPushSupported(isSupported());
|
||||
// For Electron, we assume notifications are enabled if supported
|
||||
// For web browsers, we check if there's a subscription
|
||||
setPushNotificationsEnabled(isSupported());
|
||||
}, []);
|
||||
|
||||
const handlePanelChange = (panelId: string) => {
|
||||
@@ -27,19 +30,26 @@ export function SettingsDialog({ isOpen, onOpenChange }: DialogProps) {
|
||||
|
||||
try {
|
||||
if (enabled) {
|
||||
await pushNotificationManager.initialize();
|
||||
const permission = await pushNotificationManager.requestPermission();
|
||||
|
||||
if (permission === "granted") {
|
||||
const subscription = await pushNotificationManager.subscribe();
|
||||
if (subscription) {
|
||||
await pushNotificationManager.sendSubscriptionToServer(user.authToken);
|
||||
setPushNotificationsEnabled(true);
|
||||
const initialized = await initialize();
|
||||
if (initialized) {
|
||||
await subscribe(user.authToken);
|
||||
|
||||
// For Electron, start the notification receiver
|
||||
if (isElectron) {
|
||||
await startElectronReceiver();
|
||||
}
|
||||
|
||||
setPushNotificationsEnabled(true);
|
||||
}
|
||||
} else {
|
||||
await pushNotificationManager.unsubscribe();
|
||||
// Call API to unsubscribe on server
|
||||
await unsubscribe();
|
||||
|
||||
// For Electron, stop the notification receiver
|
||||
if (isElectron) {
|
||||
stopElectronReceiver();
|
||||
}
|
||||
|
||||
// Call API to unsubscribe on server (for web browsers)
|
||||
await fetch(`${API_BASE_URL}/push/unsubscribe`, {
|
||||
method: "DELETE",
|
||||
headers: getAuthHeaders(user.authToken)
|
||||
@@ -47,7 +57,7 @@ export function SettingsDialog({ isOpen, onOpenChange }: DialogProps) {
|
||||
setPushNotificationsEnabled(false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to toggle push notifications:", error);
|
||||
console.error("Failed to toggle notifications:", error);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ import { useRef } from "react";
|
||||
import type { TextField } from "mdui/components/text-field";
|
||||
import { useAppState } from "../state";
|
||||
import { MaterialTextField } from "../components/core/TextField";
|
||||
import { pushNotificationManager } from "../../utils/pushNotifications";
|
||||
import { initialize, isSupported, startElectronReceiver, subscribe } from "../../utils/notifications";
|
||||
import { isElectron } from "../../electron/electron";
|
||||
|
||||
export default function LoginScreen() {
|
||||
const [alerts, updateAlerts] = useImmer<Alert[]>([]);
|
||||
@@ -68,26 +69,27 @@ export default function LoginScreen() {
|
||||
|
||||
setCurrentPage("chat");
|
||||
|
||||
// Initialize push notifications
|
||||
// Initialize notifications
|
||||
try {
|
||||
if (pushNotificationManager.isSupported()) {
|
||||
await pushNotificationManager.initialize();
|
||||
const permission = await pushNotificationManager.requestPermission();
|
||||
|
||||
if (permission === "granted") {
|
||||
const subscription = await pushNotificationManager.subscribe();
|
||||
if (subscription) {
|
||||
await pushNotificationManager.sendSubscriptionToServer(data.token);
|
||||
console.log("Push notifications enabled");
|
||||
if (isSupported()) {
|
||||
const initialized = await initialize();
|
||||
if (initialized) {
|
||||
await subscribe(data.token);
|
||||
|
||||
// For Electron, start the notification receiver
|
||||
if (isElectron) {
|
||||
await startElectronReceiver();
|
||||
}
|
||||
|
||||
console.log("Notifications enabled");
|
||||
} else {
|
||||
console.log("Push notification permission denied");
|
||||
console.log("Notification permission denied");
|
||||
}
|
||||
} else {
|
||||
console.log("Not supported");
|
||||
console.log("Notifications not supported");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Push notification setup failed:", e);
|
||||
console.error("Notification setup failed:", e);
|
||||
}
|
||||
} else {
|
||||
const data: ErrorResponse = await response.json();
|
||||
|
||||
Reference in New Issue
Block a user