feat: clickable notification

This commit is contained in:
zerosoul
2022-04-06 16:25:37 +08:00
parent 07ee13b8ba
commit b4db1da483
2 changed files with 82 additions and 6 deletions
+66 -2
View File
@@ -22,16 +22,22 @@ firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging(); const messaging = firebase.messaging();
console.log("wwwwwwwwww"); console.log("wwwwwwwwww");
// Handle incoming messages while the app is not in focus (i.e in the background, hidden behind other tabs, or completely closed). // Handle incoming messages while the app is not in focus (i.e in the background, hidden behind other tabs, or completely closed).
messaging.onBackgroundMessage(function (payload) { // data:{from_server_id}
messaging.onBackgroundMessage((payload) => {
console.log("Received background message ", payload); console.log("Received background message ", payload);
const notificationTitle = payload.notification.title; const notificationTitle = payload.notification.title;
const notificationOptions = { const notificationOptions = {
data: payload.data,
body: payload.notification.body, body: payload.notification.body,
}; };
self.registration.showNotification(notificationTitle, notificationOptions); self.registration.showNotification(notificationTitle, notificationOptions);
}); });
// setTimeout(() => {
// console.log("notification test");
// self.registration.showNotification("hello", { body: "test" });
// }, 5000);
// 开始监听推送 // 开始监听推送
// self.addEventListener("push", function (event) { // self.addEventListener("push", function (event) {
// var data = event.data.json(); // var data = event.data.json();
@@ -45,6 +51,64 @@ messaging.onBackgroundMessage(function (payload) {
// event.waitUntil(self.registration.showNotification(title, options)); // event.waitUntil(self.registration.showNotification(title, options));
// }); // });
// self.addEventListener("notificationclick", function (event) {}); self.addEventListener("notificationclick", function (event) {
console.log("notification click", event, event.notification);
event.waitUntil(
(async function () {
const allClients = await clients.matchAll({
includeUncontrolled: true,
});
const [firstClient] = allClients;
// 没有数据
if (!event.notification.data) {
firstClient.focus();
return;
}
const {
rustchat_from_uid,
rustchat_to_uid,
rustchat_to_gid,
} = event.notification.data;
let chatClient;
let redirectPath = rustchat_to_uid
? `/chat/dm/${rustchat_from_uid}`
: rustchat_to_gid
? `/chat/channel/${rustchat_to_gid}`
: "";
if (!redirectPath) {
firstClient.focus();
return;
}
if (allClients.length == 0) {
chatClient = await clients.openWindow(redirectPath);
} else {
firstClient.postMessage({ newPath: redirectPath });
firstClient.focus();
}
// // Let's see if we already have a chat window open:
// for (const client of allClients) {
// const url = new URL(client.url);
// if (url.pathname == '/chat/') {
// // Excellent, let's use it!
// client.focus();
// chatClient = client;
// break;
// }
// }
// // If we didn't find an existing chat window,
// // open a new one:
// if (!chatClient) {
// chatClient = await clients.openWindow('/chat/');
// }
// // Message the client:
// chatClient.postMessage("New chat messages!");
})()
);
});
// self.addEventListener("notificationclose", function (event) {}); // self.addEventListener("notificationclose", function (event) {});
+16 -4
View File
@@ -1,10 +1,11 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
// import toast, { Toaster } from 'react-hot-toast'; // import toast, { Toaster } from 'react-hot-toast';
import { useDeviceToken, onMessageListener } from "./firebase"; import { useDeviceToken, onMessageListener } from "./firebase";
import { useUpdateDeviceTokenMutation } from "../../../app/services/auth"; import { useUpdateDeviceTokenMutation } from "../../../app/services/auth";
const Notification = () => { const Notification = () => {
const navigateTo = useNavigate();
const token = useDeviceToken( const token = useDeviceToken(
`BGXCn-5YRXSFw38Q9lUKJ5bibL212-yIQn1pCvthGhp6_KwA29FO1Ax_d_7if1vfC2a5wTSVO8AcZrc-Hm1aS0Y` `BGXCn-5YRXSFw38Q9lUKJ5bibL212-yIQn1pCvthGhp6_KwA29FO1Ax_d_7if1vfC2a5wTSVO8AcZrc-Hm1aS0Y`
); );
@@ -26,9 +27,20 @@ const Notification = () => {
}, [token]); }, [token]);
useEffect(() => { useEffect(() => {
if (notification?.title) { const handleServiceworkerMessage = (event) => {
console.log("notification", notification); const { newPath } = event.data;
} navigateTo(newPath);
};
navigator.serviceWorker.addEventListener(
"message",
handleServiceworkerMessage
);
return () => {
navigator.serviceWorker.removeEventListener(
"message",
handleServiceworkerMessage
);
};
}, [notification]); }, [notification]);
onMessageListener() onMessageListener()