Firebase Cloud Messaging push notification not sending to device

This is what my log looks like when my push notification is called

I am currently working on creating custom push notifications customized for iPhone custom settings. I'm currently using Firebase, so I naturally turned to Firebase Cloud Messaging to do this. This is my setup in the functions that I am deploying to my Firebase. Is there something I am doing wrong here that will result in the notification not being sent to the device? I appreciate any help and if there is any more necessary information I would be happy to provide it.

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// Listens for new messages added to messages/:pushId
exports.pushNotification =     functions.database.ref('/messages/{pushId}').onWrite( event => {

console.log('Push notification event triggered');

//  Grab the current value of what was written to the Realtime Database.
var valueObject = event.data.val();
console.log(valueObject)

if(valueObject.photoUrl != null) {
  valueObject.photoUrl= "Sent you a photo!";
}

 // Create a notification
const payload = {
    notification: {
        title:valueObject.toId, 
        body: valueObject.text || valueObject.photoUrl,
        sound: "default"
    },
};

//Create an options object that contains the time to live for the notification and the priority
const options = {
    priority: "high",
    timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToTopic("pushNotifications", payload, options);
 if(!data.changed()){

});

exports.pushNotification = functions.database.ref('/messages/{pushId}').onWrite( event => {
const data = event.data;
console.log('Push notification event triggered');
return;
}



});

      

+3


source to share


2 answers


I noticed that you are exposing the same function twice. This is also a problem. Also I suggest you promise admin.messaging so you can handle and check for errors.



let topic = "pushNotifications";
admin.messaging().sendToTopic(topic, payload, options)
        .then(function(response) {

            console.log("Successfully sent message:", response);
            console.log("Topic: " + topic);
            res.status(200).send("success");
        })
        .catch(function(error) {
            console.log("Error sending message:", error);
            res.status(500).send("failure");
        });

      

0


source


Send this jsone by post parameter in registration_ids field, you have to post the All All token array you want to send push notification This is the body of the request method on request

{ "registration_ids" : [Send Array of Device Token], 
  "data" :
    {
    "image_url" : "send your image here"
    "message" : "Send your message here" },
 "notification" : 
   {
    "title" : "APP Name",
    "sound" : "default",
    "priority" : "high"
   }
}

      

Here is the url of the post

https://fcm.googleapis.com/fcm/send



and send key="Your Authorization key"

in the request field HttpHeader

Note the basic setup form here for cloud messaging

https://firebase.google.com/docs/cloud-messaging/ios/client

-1


source







All Articles