簡體   English   中英

Firebase雲消息傳遞requireInteraction不起作用

[英]Firebase Cloud Messaging requireInteraction not work

參考: https : //github.com/firebase/quickstart-js/tree/master/messaging

我添加了鍵值對:

"requireInteraction": true

但是20秒鍾后,桌面Chrome中的通知仍會消失。 有誰知道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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM