简体   繁体   中英

Why im getting an error in my javascript firebase cloud messaging function?

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

在此处输入图像描述

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.

comes out after 5 days of struggling that adding this statement

token:token

into payload but outside of all other statements, fixed the issue. Hope this will help someone to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM