FCM : getting ' Error: data must be a non-null object' error?
Asked Answered
S

2

8

I am trying to send a push notification via Firebase cloud messaging. I am using Firebase admin sdk to send push notification in fcm . I am using nodejs

When I am trying to send a push msg , ...

I am getting this error

{ 
    code: 'messaging/invalid-payload',
    message: 'data must be a non-null object' },
   codePrefix: 'messaging'
 }

My code :

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

const serviceAccount = require(`${__dirname}/fet_firebase.json`);

    function sendPushNot(to, body, sendId, type) {
    
      const registrationToken = to;
      const notification = {};
    
      let message = { };
      const pbody = { body };
    
      if (type === 'User') {
        pbody.userId = sendId;
        notification.userId = sendId;
        notification.title = 'New user Follwed';
      }
      if (type === 'Post') {
        pbody.postId = sendId;
        notification.postId = sendId;
        notification.title = 'Post Liked';
      }
      if (type === 'Room') {
        pbody.roomId = sendId;
        notification.roomId = sendId;
        notification.title = 'New Chat messsage';
      }
    
      message = {
        data: JSON.stringify(pbody),
        token: registrationToken,
        notification
      };
    
      console.log('messgae',message);
    
      admin.messaging().send(message)
      .then((response) => {
        // Response is a message ID string.
        console.log('Successfully sent cloud message:', response);
      })
      .catch((error) => {
        console.log('Error sending cloud message:', error);
      });
    }

I thought that body is null

But the console output of console.log('messgae',message); is ::

{
 data:
     '{"body":"pankaj Liked Your Post","postId":"5ed1055ddf0efd2a42f6a28a"}',
   token:
   'f2umP-jfQyeM1suN77zz7-:APA91bHKzfqfRnmuBom2PIDB8cCPwZtq28JCWLSi1OMPO55JRzyhFZJpTkkNyDu_StTYID-scu-grejaxxn3d4iR6Xidz9-JCk_h-bRsdGHe8nzMrIVsc8vZDFgayiFgJrJ53DaDzb9b',
  notification: { postId: 5ed1055ddf0efd2a42f6a28a, title: 'Post Liked'
 } 
}

So the body is not null

But I am getting data must be a non-null object' error ..

Why?

Shoa answered 23/6, 2020 at 10:39 Comment(0)
M
2

Data must be a non-null object. Above code sample is passing a string. Just remove the JSON.stringify() part.

Minimalist answered 23/6, 2020 at 17:59 Comment(1)
Then you probably have non-string values in your object. Data object can only have string values. Type signature: data?: { [key: string]: string };Minimalist
A
11

I fixed this by wrapping the stringified object with curly braces

data : { data: JSON.stringify(object) } // correct
data : JSON.stringify(object) // will result to the described error.
Asante answered 10/11, 2020 at 15:10 Comment(3)
That looks like SyntaxError to me. Can you please elaborate on your answer?Quiet
I believe he meant = instead of :Perilous
@emanuelsanga no, it's already correct.. you just have to make sure that there is no white space between data and : . So, it must be data : { data: JSON.stringify(object) } no data : { data : JSON.stringify(object) }Ceres
M
2

Data must be a non-null object. Above code sample is passing a string. Just remove the JSON.stringify() part.

Minimalist answered 23/6, 2020 at 17:59 Comment(1)
Then you probably have non-string values in your object. Data object can only have string values. Type signature: data?: { [key: string]: string };Minimalist

© 2022 - 2024 — McMap. All rights reserved.