Javascript 从后台处理程序中运行的firebase云消息自定义通知
如何自定义由web应用程序中Firebase的后台处理程序创建的通知 我编辑的一些示例代码如下所示:Javascript 从后台处理程序中运行的firebase云消息自定义通知,javascript,firebase,web-applications,firebase-cloud-messaging,service-worker,Javascript,Firebase,Web Applications,Firebase Cloud Messaging,Service Worker,如何自定义由web应用程序中Firebase的后台处理程序创建的通知 我编辑的一些示例代码如下所示: messaging.setBackgroundMessageHandler(function(payload) { console.log('[firebase-messaging-sw.js] Received background message ', payload); // Customize notification here const data = payload.d
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const data = payload.data;
const click_action = "https://www.google.com";
const notificationTitle = data.title;
const notificationOptions = {
"body": data.body,
"icon": "icon.png",
"click_action": click_action
};
const timeout = data.timeout;
// var n = new Notification(notificationTitle, notificationOptions);
// n.onclick = function () {
// if (click_action !== null) {
// window.open(click_action);
// }
// };
// setTimeout(n.close.bind(n), 5000);
return self.registration.showNotification(notificationTitle, notificationOptions);
});
我想设置超时选项。当我在内容脚本中创建通知时,我使用注释掉的代码来指定超时。但是,我不能在这里应用相同的技术
我还想自定义单击操作。在notification options对象中指定click_操作键对我没有任何作用。在内容脚本中,我将添加一个onClickListener
总的来说,我需要一种方法来添加一个单击侦听器并自定义超时持续时间。通过向JSON负载添加“time\u to\u live”:3
来指定FCM中消息的寿命
您可以在以下网站上查看更多详细信息:
希望这有助于在JSON负载中添加“time to_live”:3。