Я использую Vue js на стороне клиента и узел js на стороне сервера. Я отправляю сообщение с данными, и оно будет получено клиентской стороной, когда вкладка браузера неактивна. Но когда вкладка активна, она не получает уведомления.
Вот мой код на стороне клиента
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "<apiKey>",
authDomain: "<authDomain>",
databaseURL: "<db>",
projectId: "<projectId>",
storageBucket: "<storageBucket>",
messagingSenderId: "<senderId>",
appId: "<appId>"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Retrieve Firebase Messaging object.
const messaging = firebase.messaging();
messaging.usePublicVapidKey(
"<KEY>"
);
messaging
.requestPermission()
.then(function() {
retrieveToken();
console.log("Notification permission granted.");
})
.catch(function(err) {
console.log("Unable to get permission to notify. ", err);
});
function retrieveToken() {
messaging
.getToken()
.then(deviceToken => {
if (deviceToken) {
console.log(deviceToken);
messaging.onMessage(payload => {
console.log("Message received. ", payload);
});
} else {
console.log(
"No Instance ID token available. Request permission to generate one."
);
}
})
.catch(err => {
console.log("An error occurred while retrieving token. ", err);
});
}
}
</script>
А вот мой код на стороне сервера
var message = {
data: {
score: "850",
time: "2:45"
},
token: registrationToken
};
// Send a message to the device corresponding to the provided
// registration token.
admin
.messaging()
.send(message)
.then(response => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch(error => {
console.log("Error sending message:", error);
});