Sam*_*Lui 2 javascript google-chrome push-notification firebase firebase-cloud-messaging
参考:https://github.com/firebase/quickstart-js/tree/master/messaging
我添加了键值对:
"requireInteraction": true
Run Code Online (Sandbox Code Playgroud)
但桌面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"
Run Code Online (Sandbox Code Playgroud)
Firebase 在传递消息时requireInteraction从notification有效负载中剥离属性.有效的解决方法是使用data属性而不是notification.然后,您可以根据需要使用该setBackgroundMessageHandler()方法构建通知:
messaging.setBackgroundMessageHandler(function (payload) {
return self.registration.showNotification(payload.data.title,
Object.assign({data: payload.data}, payload.data));
});
Run Code Online (Sandbox Code Playgroud)
我已经设置了data上面,因为click_action不再使用这种方法,你需要自己注册所需的onclick处理程序.这是一个工作服务工作者,它完全按照您的意图执行notification,但使用数据属性:
// 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]
Run Code Online (Sandbox Code Playgroud)
这将是你的卷曲电话:
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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1274 次 |
| 最近记录: |