Пробовал использовать облачные сообщения Firebase и успешно отправлял сообщения, используя метод POST HTTPs и из серверной части. Статус показывает, что сообщение было успешно отправлено, но я все еще не получил никаких уведомлений от веб-приложения. Ниже приведены результаты после отправки сообщений:
Использование приложения PostMan
Из серверной части
Ниже приведены мои коды:
firebase-messaging-sw. js
importScripts("https://www.gstatic.com/firebasejs/7.6.1/firebase-messaging.js");
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("../firebase-messaging-sw.js")
.then(function (registration) {
console.log("Registration successful, scope is:", registration.scope);
})
.catch(function (err) {
console.log("Service worker registration failed, error:", err);
});
}
firebase.initializeApp({
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
});
const initMessaging = firebase.messaging();
firebase. js
import firebase from "firebase";
const config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
};
try {
firebase.initializeApp(config);
} catch (error) {
console.log(error);
}
export default firebase;
отправить. js (для отправки сообщений)
const admin = require("firebase-admin");
const serviceAccount = require("./service-account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://firebaseio.com",
});
const registrationToken =
"dVzuab-HgqTEi8FtcxIiw_:APA91bFbm5gqhxINk8uDrRjpHFq3OxJWTNzl8O31zuJzcSWgJhodBoEEqqbuflp-kqkusyyGZPZ7j8b-t9uknljZlOjOFl6Re70ty4wD96hdVBir6At9n8gwMIHb961anuLoxqBSN2EL";
const payload = {
data: {
Title: "Helloooooo",
},
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24,
};
const message = {
data: {
title: "Hello",
body: "Hello World",
},
token: registrationToken,
};
admin
.messaging()
.sendToDevice(registrationToken, payload, options)
.then(function (response) {
console.log("Successfully sent message", response);
})
.catch(function (error) {
console.log("Error sending message: ", error);
});
admin
.messaging()
.send(message)
.then((response) => {
console.log("Successfully sent message: ", response);
})
.catch((error) => {
console.log("Error sending message: ", error);
});
Пожалуйста, помогите! Спасибо!