【问题标题】:Firebase Cloud Messaging requireInteraction not workFirebase 云消息传递 requireInteraction 不起作用
【发布时间】:2017-02-06 08:52:27
【问题描述】:

参考: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"

【问题讨论】:

  • 嗨塞缪尔。我不认为requireInteraction 是你应该在你的有效载荷中设置的东西。它应该只在您 构建 通知时声明。如果问题只是被支持,那么requireInteraction 不属于notification 有效负载,但可以存在data 有效负载中。 :)
  • @AL。我尝试在 data payload 或 notification payload 中添加 requireInteraction: true ,但我的 chrome 桌面中的通知弹出窗口在 20 秒后仍然消失
  • 以下演示中的弹出窗口之一在用户单击之前不会关闭。 googlechrome.github.io/samples/notifications/…

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


【解决方案1】:

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"

【讨论】:

  • 这是否仍然正确,我似乎无法让通知持续存在?我使用的是 firebase 4.3.0,上面的 curl 请求和我的唯一区别是我仍然有通知有效负载 - 所以我有数据和通知
  • 我认为你需要决定一个。如果我没记错的话,通知具有优先权,并且只有在没有通知有效负载的情况下才会使用数据。
  • 非常感谢,老兄 - 删除了通知内容,它就像一个魅力
  • @bambam 这很完美.. 但是有什么方法可以让通知在应用处于焦点时显示?
  • @bambam:知道如何在用户清除缓存后获取新令牌。还有任何想法获取唯一的浏览器 ID 以保存令牌以避免重复令牌。谢谢
猜你喜欢
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2018-01-09
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多