Javascript Web推送通知'requireInteraction'不工作
我已尝试将requireInteraction设置为true 我在“messageHandler”和“BackgroundMessageHandler”中都这样做了 几秒钟后警报仍然消失。我使用的是同一台设备,使用Chrome,从另一个网站推送“粘贴”通知,所以这是一个代码问题 我可能做错了什么 以下是我的notifications.js脚本:Javascript Web推送通知'requireInteraction'不工作,javascript,push-notification,notifications,web-push,Javascript,Push Notification,Notifications,Web Push,我已尝试将requireInteraction设置为true 我在“messageHandler”和“BackgroundMessageHandler”中都这样做了 几秒钟后警报仍然消失。我使用的是同一台设备,使用Chrome,从另一个网站推送“粘贴”通知,所以这是一个代码问题 我可能做错了什么 以下是我的notifications.js脚本: const firebaseConfig = { apiKey: "zzzzzzzzzzzz", authDomain: &
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();