我在chrome扩展程序中使用了fcm。现在我的firebase-messaging-sw.js已被激活并且可以正确调用'setBackgroundMessageHandler',但是当popup.html打开时仍然会调用'setBackgroundMessageHandler'回调。 messages()。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"}
顺便说一句,我没有在manifest.json中放入'gcm_sender_id',因为chrome不允许这样做,这有什么关系吗?