Firebase messaging (). OnMessage () не вызывается при открытом popup.html - PullRequest
0 голосов
/ 27 апреля 2019

Я использовал fcm в своем расширении chrome. Теперь мой firebase-messaging-sw.js активирован, и «setBackgroundMessageHandler» может быть вызван правильно. обратный вызов messaging (). onMessage никогда не вызывается. Как это исправить?

Вот мой код.


// firebase-messaging-sw.js
....
messaging.setBackgroundMessageHandler(function(payload) {
    console.log('[firebase-messaging-sw.js] Received background message ', payload);
    // Customize notification here
    let notificationTitle = 'Background Message Title';
    let notificationOptions = {
        body: 'Background Message body.',
        icon: 'resource/icon_web.png'
    };

    return self.registration.showNotification(notificationTitle, notificationOptions);
});
// popup.js
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.onMessage(function (payload) {
  console.log('Message received. ', payload);
});
// request
curl -X POST \
  https://fcm.googleapis.com/fcm/send \
  -H 'Authorization: key=<xxx>' \
  -H 'Content-Type: application/json' \
  -d '{
   "data": {
      "title":"postman data title",
      "body": "postman data body"
   },
   "to":"<xxxx>"
}'

Консоль:

[firebase-messaging-sw.js] Received background message  
{data: {…}, from: "153493200810", collapse_key: "do_not_collapse"}

Кстати, я не поместил 'gcm_sender_id' в файл manifest.json, потому что Chrome не позволяет этого.

...