我正在向 Web 应用程序发送 fcm 消息,我可以在后台接收消息,但是当我单击警报时,它不会在浏览器中打开 URL,一切正常,只是链接不起作用。
这是我发送的消息,请帮忙
{
"notification": {
"title": "Hello World",
"body": "Enjoy your coffee"
},
"webpush": {
"fcm_options": {
"link": "https://weather.com/en-CA/weather/today/l/CAON4756:"
}
}
"to" : ""
}
我的火碱消息传递.js文件
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-messaging.js');
firebase.initializeApp({
'messagingSenderId': ''
});
const messaging = firebase.messaging();
只能打开与注册service worker
具有相同host
的link
。
在您的消息中,host
是weather.com
感知不到您注册service worker
host
。
我打开第三方站点的解决方案是设置url param
。
溶液
JavaScript insomepage
let urlToOpen = getQueryVariable(window.location.search.substring(1), "url")
if (urlToOpen) {
window.open(urlToOpen, "_self")
}
function getQueryVariable(query, variable) {
const vars = query.split("&")
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split("=")
if (pair[0] === variable) {
return pair[1]
}
}
return false
}
要发送的消息,只需将"url2somepage"
替换为要somepage
的 url(host
与service worker
相同(
{
"notification": {
"title": "Hello World",
"body": "Enjoy your coffee"
},
"webpush": {
"fcm_options": {
"link": "url2somepage" + "?url=" + "https://weather.com/en-CA/weather/today/l/CAON4756:"
// replace the 'url2somepage' with the url to a page which contains the previous js
}
}
"to" : ""
}