Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Payload error in Cloud Functions for Firebase Messaging

I wrote a cloud function using Cloud Functions for Firebase that sends notifications to certain topics of Firebase Messaging. The final parts of the function define the payload to be sent, then sends it:

// javascript code in cloud functions
const payload = {
      'notification': {
        'title': `${toTitleCase(name)} just logged an event`,
        'body': `${events[eventType]} for ${toTitleCase(petName)}`,
        'data': {
            'personSent': userSent 
        }
      }
    };
console.log(payload);
admin.messaging().sendToTopic(pet_Id, payload);

However, I'm getting the error log in my Firebase console:

Error: Messaging payload contains an invalid value for the "notification.data" property. Values must be strings.

When I logout the payload I confirmed it is all strings:

{ notification: 
   { title: 'Turtle Dude just logged an event',
     body: 'Walk for Beer',
     data: { personSent: 'mfsP8U0qDdQL4rrrbXp6K0YsF423' } } }

However, when I send the same payload from my iPhone app (which I'm trying to avoid since that means I have to store the messaging private key on the client side) I am able to attach the extra data I want to send just fine:

// Swift code in iPhone app
let body: [String: Any] = ["to": "/topics/\(currentPet)",
                            "priority" : "high",
                            "notification" : [
                                "body" : "\(events[eventType]) for \(petsName.localizedCapitalized)",
                                "title" : "\(myName.localizedCapitalized) just logged an event",
                                "data" : ["personSent": myId]
                              ]
                           ]

How can I accomplish adding additional data in my cloud function like I do in my Swift code?

like image 717
MarksCode Avatar asked Mar 29 '17 20:03

MarksCode


People also ask

What is payload in Firebase?

Data Payload: Data messages have to be handled by the android app. You can add this kind of messages if you want to send some only data along with the notification. It contains custom key value pairs.

Is there any limit for Firebase Cloud Messaging?

You can send up to 240 messages/minute and 5,000 messages/hour to a single device.

What are the two types of messages in Firebase Cloud Messaging?

Using Firebase Cloud Messaging, we can send three types of messages, i.e., Notification Message, Data Message, and the message with both Notification & Data Payload.


1 Answers

As others explained in the comments, the data object should go inside payload,
NOT inside notification.

Try the following code:

// javascript code in cloud functions
const payload = {
      'notification': {
        'title': `${toTitleCase(name)} just logged an event`,
        'body': `${events[eventType]} for ${toTitleCase(petName)}`,
      }, 
      // NOTE: The 'data' object is inside payload, not inside notification
      'data': { 
            'personSent': userSent 
      }
    };
console.log(payload);
admin.messaging().sendToTopic(pet_Id, payload);
like image 50
Diego Giorgini Avatar answered Oct 26 '22 08:10

Diego Giorgini