This is my first time using firebase. I am integrating Firebase Cloud Message with a node.js server and an Android client. When I push the messages to the server, the response is that the message went to the firebase server successfully.
But when I go to the firebase console, I don't see my message there and also my android device does not receive the message. I am sending the messages as topics and the android device has also subscribed to the topics.
I will be glad if someone can share a link to view the messages on the server or suggest what I am missing out.
Below is a sample response: Message sent to Firebase for delivery, response: { "name": "projects/my-project-id/messages/7660010658785245660" }
Below is the code that I am using to push to the server
let admin = require('firebase-admin'),
serviceAccount = require('the link to my service account is here'),
fcmObj = {};
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
fcmObj.sendMsg = function(){
let options = {
priority: "high",
timeToLive: 60 * 60 *24
},
message = {
data: {
content: 'Hello.. we are testing our api and fcm.',
sender: 'From Server'
},
topic: "News"
};
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message, options)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', JSON.stringify(response));
})
.catch((error) => {
console.log('Error sending message:', error);
});
}
module.exports = fcmObj;
Below is a sample response: Message sent to Firebase for delivery, response: { "name": "projects/my-project-id/messages/7660010658785245660" }
Means that your Firebase SDK is is sending the message successfully. (i.e. credentials are configured properly and SDK is initialised successfully.)
In your code, you've defined data( payload is optional, see documentation here: https://firebase.google.com/docs/cloud-messaging/concept-options#notification-messages-with-optional-data-payload), to display the notification message you need to add a notification object.
Just add following object in your message
object and it should work then:
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With