我正在学习Firebase并遇到问题:我无法发送通知。我尝试使用onMessage
,但是什么也没有发生。
var message = {
content_available: false,
notification: {
title: "My first Push",
body: "My first Push",
},
to: token
};
global.admin.messaging().onMessage(function(message) {
const notificationTitle = message.notification.title;
if (!("Notification" in window)) {
console.log("This browser does not support system notifications");
} else if (Notification.permission === "granted") {
var notification = new Notification(notificationTitle, message.notification);
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open(event.notification.click_action, '_blank');
notification.close();
}
}
console.log('Message received. ', message);
})
.then((response) => {
console.log("Sucsessfully sent message:", response);
console.log(response.results[0].error);
})
.catch((error) => {
console.log("Error sending message:", error);
})