我正在使用fcm发送推送通知。 我的有效负载就是这个,我可以在console.log中看到它,但是这些url值始终是不确定的。
这是完整的有效负载,这是serviceworker.js
有效载荷
{title: "test", body: "This is the body content", icon: "https://png.pngtree.com/element_origin_min_pic/16/08/05/1057a3fae73b91b.jpg", image: "https://png.pngtree.com/element_origin_min_pic/16/08/05/1057a3fae73b91b.jpg", badge: "https://cdn.iconscout.com/icon/free/png-128/love-r…antic-valentine-valentines-day-heart-10-24261.png", …}badge: "https://cdn.iconscout.com/icon/free/png-128/love-romance-romantic-valentine-valentines-day-heart-10-24261.png"body: "This is the body content"icon: "https://png.pngtree.com/element_origin_min_pic/16/08/05/1057a3fae73b91b.jpg"image: "https://png.pngtree.com/element_origin_min_pic/16/08/05/1057a3fae73b91b.jpg"title: "chutiyaaaaaa"url: "https://ndtv.com/7877"
serviceworker.js
self.addEventListener('push', function (event) {
if (!(self.Notification && self.Notification.permission === 'granted')) {
return;
}
const sendNotification = body => {
// you could refresh a notification badge here with postMessage API
const title = "Web Push example";
// const title = "Web Push example";
const options999999 = {
icon: 'https://png.pngtree.com/element_origin_min_pic/16/08/05/1057a3fae73b91b.jpg',
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
primaryKey: 1
},
actions: [
{action: 'explore', title: 'Explore this new world',
icon: 'images/checkmark.png'},
{action: 'close', title: 'I don\'t want any of this',
icon: 'https://png.pngtree.com/element_origin_min_pic/16/08/05/1057a3fae73b91b.jpg'}
]
};
return self.registration.showNotification(title, {
body,
});
};
if (event.data) {
var payload = event.data.json();
var title = payload.title;
var body = payload.body;
var bigimg = payload.image;
var badgee = payload.badge;
var url = payload.url;
var icon = payload.icon;
console.log(payload);
event.waitUntil(self.registration.showNotification(title, {
body: body,
icon: icon,
image: bigimg,
badge: badgee,
data: {
url: url
},
data: {} // Keeping this here in case I need it later
}
));
}
});
self.onnotificationclick = function(event) {
var url99 = '';
const notificationpp = event.notification;
const primaryKey = notificationpp.data.url;
const notificationpp999 = event.notification.data;
event.notification.close();
let clickResponsePromise = Promise.resolve();
if (event.notification.data && event.notification.data.url) {
clickResponsePromise = clients.openWindow(event.notification.data.url);
}
event.waitUntil(
clients.matchAll({
type: 'window'
})
.then(function() {
if (clients.openWindow) {
var payload22 = event.notification.data.url;
url99 = event.notification.data.url;
console.log(payload22); // why undefined ??
return clients.openWindow(payload22); // open it
}
})
);
};
问题出在哪里?当我单击通知时,它始终显示未定义,但是我通过了正确的参数器