#javascript #push-notification #notifications #web-push
Вопрос:
Я попытался установить requireInteraction в значение true.
Я сделал это как в «MessageHandler», так и в «BackgroundMessageHandler».
Предупреждения по — прежнему исчезают через несколько секунд. Я получаю на том же устройстве, используя Chrome, push-уведомления с другого веб — сайта, которые «прилипают», так что это проблема с кодом.
Что я могу сделать не так?
Вот мой notifications.js сценарий:
const firebaseConfig = {
apiKey: "zzzzzzzzzzzz",
authDomain: "zzzzzzzzzz.firebaseapp.com",
projectId: "zzzzzzzzzzz",
storageBucket: "zzzzzzzzz.appspot.com",
messagingSenderId: "zzzzzzzzzzz",
appId: "1:zzzzzzz:web:zzzzzzzzzzzzzzz",
},
vapidKey =
"zzzzzzzzzzzz";
function messageHandler(payload) {
navigator.serviceWorker.register("firebase-messaging-sw.js");
navigator.serviceWorker.ready.then(function (registration) {
return registration.showNotification(
payload.notification.title,
payload.notification
);
});
}
function saveToken(token) {
const body = new FormData();
body.append("client_push_token", token);
body.append("token", localStorage.getItem("session_id"));
body.append("visit_id", localStorage.getItem("visit_id"));
fetch("/test/save-token.php", { method: "POST", body });
}
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
messaging.onMessage(messageHandler);
messaging
.getToken({ vapidKey })
.then(saveToken)
.catch((err) => {
console.log("An error occurred while retrieving token. ", err);
});
И мой скрипт обмена сообщениями firebase:
importScripts("https://www.gstatic.com/firebasejs/8.3.0/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/8.3.0/firebase-messaging.js");
const firebaseConfig = {
apiKey: "zzzzzzzzzzzz",
authDomain: "zzzzzzzzzz.firebaseapp.com",
projectId: "zzzzzzzzzzz",
storageBucket: "zzzzzzzzz.appspot.com",
messagingSenderId: "zzzzzzzzzzz",
appId: "1:zzzzzzz:web:zzzzzzzzzzzzzzz",
};
self.addEventListener('notificationclick', function (e) {
const
notification = e.notification.data.FCM_MSG.notification,
[button1, button2] = notification.actions,
id = notification.id,
eventAction = e.action;
fetch(`/test/click-handler.php?id=${id}`);
if (eventAction === button1.action) {
clients.openWindow(button1.url);
} else if(eventAction === button2.action) {
clients.openWindow(button2.url);
} else {
clients.openWindow(notification.click_action);
}
e.notification.close();
});
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();