Firebase云消息传递requireInteraction不起作用 [英] Firebase Cloud Messaging requireInteraction not work
问题描述
参考: https://github.com/firebase/quickstart-js / tree / master / messaging
我已经添加了键值对:
<$ p $但是桌面版Chrome中的通知仍然不存在20秒后。有谁知道Firebase是否支持这个键值对?谢谢!
我的例子如下。请将
更改为您的。
curl -X POST -H授权:key = [...]-HContent-Type:application / json-d{
notification:{
requireInteraction:true,
title:This is custom title,
body:this is custom body,
click_action:https://google.com,
data:{requireInteraction:true}
},
to:[...],
}'https://fcm.googleapis.com/ fcm / send
/ code>
当消息被传递时,来自通知
有效载荷中的requireInteraction 属性。可行的解决方法是使用 data
属性而不是通知
。然后,您可以使用 setBackgroundMessageHandler()
方法构建您想要的通知:
messaging.setBackgroundMessageHandler(function(payload){
return self.registration.showNotification(payload.data.title,
Object.assign({data:payload.data},payload .data));
});
我已经在上面设置了 data
,因为 click_action
不再适用于此方法,您需要自行注册所需的onclick处理程序。这是一个正在工作的服务工作人员,它完全按照您的意图通知
,但使用data属性:
//导入脚本省略
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]
self.addEventListener('notificationclick',e => {
let found = false;
let f = clients.matchAll ({
includeUncontrolled:true,
type:'window'
})
.then(function(clientList){
for(let i = 0; i< ; clientList.length; i ++){
if(clientList [i] .url === e.notification.data.click_action){
//我们已经有一个窗口可以使用,将它聚焦。
found = true;
clientList [i] .focus();
break;
}
}
if(!found){
clients.openWindow(e.notification.data.click_action).then(function(windowClient){});
}
});
e.notification.close();
e.waitUntil(f);
});
[START background_handler]
messaging.setBackgroundMessageHandler(function(payload){
console.log('[firebase-messaging-sw.js] Received background message',有效载荷);
//在这里自定义通知
返回self.registration.showNotification(payload.data.title,
Object.assign({data:payload.data},payload .data));
});
// [END background_handler]
这将是您的卷曲调用:
curl -X POST -HAuthorization:key = yourKey--HContent-Type:application / json-d'{
data:{
title:fooTitle,
body:foo,
icon:image.jpg,
click_action:http:// localhost:8000,
requireInteraction:true
},
registration_ids:[id1,id2]
}'https://fcm.googleapis.com/fcm/send
Reference: https://github.com/firebase/quickstart-js/tree/master/messaging
I have added the key-value pair:
"requireInteraction": true
But the notification in Desktop Chrome still disappear after 20 seconds. Does any one know if Firebase supports this key-value pair? Thanks!
My example below. Please change [...]
to yours.
curl -X POST -H "Authorization: key=[...]" -H "Content-Type: application/json" -d '{
"notification": {
"requireInteraction": true,
"title": "This is custom title",
"body": "this is custom body",
"click_action": "https://google.com",
"data" : {"requireInteraction": true }
},
"to": "[...]",
}' "https://fcm.googleapis.com/fcm/send"
Firebase strips the requireInteraction
property from the notification
payload when the message is delivered. The workaround that works is to use the data
property instead of the notification
. You can then use the setBackgroundMessageHandler()
method to build the notification as you want it to be:
messaging.setBackgroundMessageHandler(function (payload) {
return self.registration.showNotification(payload.data.title,
Object.assign({data: payload.data}, payload.data));
});
I've set data
above, because the click_action
no longer works with this approach and you need to register the desired onclick handler yourself. Here's a working service worker that does exactly what you intend with your set notification
, but uses the data property instead:
// import scripts omitted
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]
self.addEventListener('notificationclick', e => {
let found = false;
let f = clients.matchAll({
includeUncontrolled: true,
type: 'window'
})
.then(function (clientList) {
for (let i = 0; i < clientList.length; i ++) {
if (clientList[i].url === e.notification.data.click_action) {
// We already have a window to use, focus it.
found = true;
clientList[i].focus();
break;
}
}
if (! found) {
clients.openWindow(e.notification.data.click_action).then(function (windowClient) {});
}
});
e.notification.close();
e.waitUntil(f);
});
// [START background_handler]
messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
return self.registration.showNotification(payload.data.title,
Object.assign({data: payload.data}, payload.data));
});
// [END background_handler]
Where this would be your curl call:
curl -X POST -H "Authorization: key=yourKey-" -H "Content-Type: application/json" -d '{
"data": {
"title": "fooTitle",
"body": "foo",
"icon": "image.jpg",
"click_action": "http://localhost:8000",
"requireInteraction": true
},
"registration_ids": ["id1", "id2"]
}' "https://fcm.googleapis.com/fcm/send"
这篇关于Firebase云消息传递requireInteraction不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!