Javascript Firebase云消息传递要求交互不工作
参考: 我已经添加了键值对:Javascript Firebase云消息传递要求交互不工作,javascript,google-chrome,firebase,push-notification,firebase-cloud-messaging,Javascript,Google Chrome,Firebase,Push Notification,Firebase Cloud Messaging,参考: 我已经添加了键值对: "requireInteraction": true 但是桌面Chrome中的通知在20秒后仍然消失。有人知道Firebase是否支持这个键值对吗?谢谢 下面是我的例子。请将[…]更改为您的 curl -X POST -H "Authorization: key=[...]" -H "Content-Type: application/json" -d '{ "notification": { "requireInteraction": true,
"requireInteraction": true
但是桌面Chrome中的通知在20秒后仍然消失。有人知道Firebase是否支持这个键值对吗?谢谢
下面是我的例子。请将[…]
更改为您的
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会将
requireInteraction
属性从通知
有效负载中剥离。有效的解决方法是使用数据
属性,而不是通知
。然后,您可以使用setBackgroundMessageHandler()
方法按照您的要求生成通知:
messaging.setBackgroundMessageHandler(function (payload) {
return self.registration.showNotification(payload.data.title,
Object.assign({data: payload.data}, payload.data));
});
我在上面设置了数据
,因为单击操作
不再适用于这种方法,您需要自己注册所需的onclick处理程序。下面是一个正在工作的服务人员,它完全按照您的设置通知所希望的方式工作,但使用数据属性:
// 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]
嗨,塞缪尔。我不认为这是你应该在你的有效载荷中设置的东西。在构建通知时应该声明它。如果问题只是关于是否支持,则requireInteraction
不属于通知
有效负载,但可以存在于数据
有效负载中:@我尝试在数据负载或通知负载中添加requireInteraction:true,但我的chrome桌面上的通知弹出窗口在20秒后仍然消失。下面演示中的弹出窗口中有一个将在用户单击后关闭。这是否仍然正确,我似乎无法获得持续的通知?我使用的是firebase 4.3.0,上面的curl请求和我的请求之间的唯一区别是,我仍然有通知负载——因此我有数据和通知,我认为您需要决定一个。如果我没记错的话,通知具有优先权,只有在没有通知负载的情况下才会使用数据。非常感谢,巴德-删除了通知内容,它就像一个charm@bambam这个很好用。。但有什么方法可以让通知在应用程序处于焦点时显示出来吗?@bambam:知道在用户清除缓存后如何获取新令牌吗。此外,为了避免令牌的重复,还可以获取唯一的浏览器id来保存令牌。非常感谢。
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"