• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

236
Views
Why im getting this error invalid Payload?
import admin from "firebase-admin";

async function notifyUser(message ,title, photoURL,token , uid , notificationsOn ) {

  const messages = [];

  const payload = {
    notification: {
      title: title,
      body: message,
    },
    data: {
      uid,
    },
    android: {
      notification: {
        image: photoURL,
      },
    },
    apns: {
      payload: {
        aps: {
          "mutable-content": 1,
        },
      },
      fcm_options: {
        image: photoURL,
      },
    },
  };

  try {
 if (token) {
     

   
  messages.push({ token: token, ...payload });
  console.log("yes");
}
    

    if (messages.length && notificationsOn==true) {
      await admin.messaging().sendAll(messages);
      console.log("Token for user, send notification.");
    }
  } catch (error) {
    const functions = require("firebase-functions");

    functions.logger.log("Hello from info. Here's an object:", error);
   

    console.log(photoURL);
  console.log(notificationsOn);
      console.log(token);
      console.log(message);
      console.log(uid);
      console.log(title)
    console.log("No token for user, can not send notification.");
   
    return {error:error.code};
  }

  return;
};

export {notifyUser} ;

The error I get

enter image description here

The line 169 is this line

 await admin.messaging().sendAll(messages);

It's always logging "No token for user cannot send notification". And I don't have any idea why. I checked like 30 times. The logs are not empty and give the correct output. The token is not empty.

It's not working on both android and iOS. But when sending notification via firebase to android or iOS it works. And notification retrieved .

Hope really anyone has and idea. I also tried to send notification from firebase via token. This also works . And as I said I checked every output it should work but does not.

If you think I did something wrong in configuration in my project please tell me what I you think it can be .

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

comes out that adding this statement

token:token like this

  const payload = {
    notification: {
      title: title,
      body: message,
    },
    token: token,
    data: {
      uid,
    },
    android: {
      notification: {
        image: photoURL,
      },
    },
    apns: {
      payload: {
        aps: {
          "mutable-content": 1,
        },
      },
      fcm_options: {
        image: photoURL,
      },
    },
  };

into payload but outside of all other statements fixed the issue.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error