Firebase functions send push notifications problem with admin.send(message)

37 Views Asked by At

There is an iOS application that receives push notifications; for this purpose, a server part was created in Node.js with Firebase cloud functions. There is a standard synchronous function for sending notifications, but it works with great loss, so we are trying to make it asynchronous, but we are faced with a problem.

Below is the code, testing on silent notifications

var allMesseges = []
const sendAllMessages = async () => {
  const sendMessege = allMesseges.map(async (message) => {
    let response = await admin.messaging().send(message);
  });
  await Promise.all(sendMessege);
};

exports.allSilentPushTrigerAsync =    functions.database.ref('/development/allSilentPushTrigerAsync/').onWrite(async event => {
  let snap = await db.ref("users").once("value")
  let users = snap.val()
  for (const [key, user] of Object.entries(users)) {
    if (user.details.pushToken) {
      const message = {
        apns: {
          headers: {
            "apns-push-type": "background",
            "apns-priority": "5",
          },
          payload: {
            "aps": {
              "content-available": 1
            },
          },
        },
        token: user.details.pushToken,
      };
      allMesseges.push(message)
    }
  }
  await sendAllMessages()
})

We get an error in the logs

allSilentPushTrigerAsync
pzhv27hvepg4
Error: Requested entity was not found.
at Function.fromServerError (/workspace/node_modules/firebase-             admin/lib/utils/error.js:254:16)
at createFirebaseError (/workspace/node_modules/firebase-admin/lib/messaging/messaging-errors-internal.js:35:47)
at /workspace/node_modules/firebase-admin/lib/messaging/messaging-api-request-internal.js:79:75
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /workspace/index.js:180:28
at async Promise.all (index 4)
at async sendAllMessages (/workspace/index.js:182:9)
at async /workspace/index.js:207:10

at async /workspace/index.js:180:28 refers to the line of code:
let response = await admin.messaging().send(message);

at async /workspace/index.js:207:10 refers to the line of code:
await sendAllMessages()
0

There are 0 best solutions below