我的服务工作者文件包含以下代码,我发送的CURL请求中包含data
且通知密钥值在内部数据中:
curl -X POST -H "Authorization: key=MY_KEY" -H "Content-Type: application/json" -d '{
"data": {
"image": "https://static.pexels.com/photos/4825/red-love-romantic-flowers.jpg",
"notification": {
"title": "Sample Title",
"body": "Sample Body",
"icon": "https://abcd.com/web-push-logo.png",
"click_action": "https://google.com",
"image": "https://abcd.com/article/4620/pictures/3336517/1.jpg"
}
}
"to": "<REGISTRATION_ID>"
}' "https://fcm.googleapis.com/fcm/send"
Inside Service Worker文件:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
res = JSON.parse(payload.data.notification)
var notificationTitle = res.title;
var notificationOptions = {
click_action: "https://google.com",
body: res.body,
icon: res.icon,
image: res.image
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
当我在数据对象中放置通知时,单击操作不起作用,如果我在数据外发送通知,则单击操作。