javascript - Firebase 云消息传递 requireInteraction 不起作用

标签 javascript google-chrome firebase push-notification firebase-cloud-messaging

引用:https://github.com/firebase/quickstart-js/tree/master/messaging

我已经添加了键值对:

"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 在传递消息时从 notification 有效负载中剥离 requireInteraction 属性。可行的解决方法是使用 data 属性而不是 notification。然后,您可以使用 setBackgroundMessageHandler() 方法来构建您希望的通知:

messaging.setBackgroundMessageHandler(function (payload) {
    return self.registration.showNotification(payload.data.title,
        Object.assign({data: payload.data}, payload.data));
});

我在上面设置了 data,因为 click_action 不再适用于这种方法,您需要自己注册所需的 onclick 处理程序。这是一个工作的服务工作人员,它完全按照您的意图使用您的设置 notification,但使用 data 属性:

// 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]

这将是您的 curl 调用:

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"

关于javascript - Firebase 云消息传递 requireInteraction 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42063675/

相关文章:

javascript - ClientId 导致 NullReferenceException

javascript - MVC 3 和 PartialViews,脚本不可见?

javascript - 视差滚动问题 - 在 webkit 浏览器中滚动时 div 元素抖动

ios - gidsignin 与 swift 和 firebase : Checking if the user is a new or returning user

javascript - 重新声明 JavaScript 变量有什么用吗?

javascript - TSconfig:为每个ts文件夹编译一个新的js文件夹

google-chrome - 仅在 Chrome 上进行 CSS3 转换(翻译)后文本变得模糊

ajax - WordPress网站从前端访问wp-admin/ajax.php页面时显示403错误

android - Flutter Firebase Messaging 无法创建服务 io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

java - Android:Firebase Cloud Messaging 不发送构建 .apk 的通知