gpt4 book ai didi

javascript - FCM Web后台服务人员

转载 作者:太空宇宙 更新时间:2023-11-04 15:26:37 24 4
gpt4 key购买 nike

当我的网页处于后台(窗口最小化或其他标签)时,我无法接收推送消息,它只显示正常的通知,但当我时,我的 page.js 中没有任何反应尝试发回帖子消息。

sw.js:

messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
const title = '¡Mensaje Nuevo!';
const options = {
body: 'conductor: ' + payload.data.chofer_id + ' - ' + payload.data.mensaje,
icon: '/icono.png',
};

send_message_to_all_clients('sound');

return self.registration.showNotification(title, options);
});

function send_message_to_all_clients(msg) {
clients.matchAll().then(clients => {
clients.forEach(client => {
send_message_to_client(client, msg).then(m => console.log("SW Received Message: " + m));
})
})
}

function send_message_to_client(client, msg) {
return new Promise(function (resolve, reject) {
var msg_chan = new MessageChannel();

msg_chan.port1.onmessage = function (event) {
if (event.data.error) {
reject(event.data.error);
} else {
resolve(event.data);
}
};

client.postMessage("SW Says: '" + msg + "'", [msg_chan.port2]);
});
}

页面.js

// Handler for messages coming from the service worker
navigator.serviceWorker.addEventListener('message', function (event) {
console.log("Client 1 Received Message: " + event.data);
$.playSound('Sounds/Tethys.mp3');
});

当我在网页上发送消息时,控制台日志会显示并播放声音:

客户端 1 收到消息:[object Object]

但永远不会在后台。

请有人帮忙,已经好几个月了。

最佳答案

终于找到我的问题了!

我正在使用通知部分发送 FCM 消息,但它阻止使用服务工作人员功能将消息发布回我的页面。

{
"to": "<token>",
"notification": { // this was the problem
"body": "test"
}
"data": {
"chofer_id": 3,
"mensaje": "hola"
}
}

关于javascript - FCM Web后台服务人员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47066544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com