简体   繁体   中英

How to solve { error: 'MismatchSenderId' }?

What im trying to do is to make push notifications using Expo Server. I have followed the expo documentation for the frontend and backend as well. https://docs.expo.dev/push-notifications/overview/ I don't have any experience with push notifications, but by far all I know is that the Expo server can take care of handling the expo token notifications, but all I have to do is just to make some configuration with the firebase to my project: https://docs.expo.dev/push-notifications/using-fcm/ Also I have run expo push:android:upload --api-key, which token is the server key I get from Firebase in could messaging

After I have created the APK, my Heroku logs(where the backend of my project is deployed) showed me this error:

 chunk [
   {
     to: 'ExponentPushToken[xxxxxxxxxxxxxxxxxx]',
     sound: 'default',
     body: 'MyApp',
     data: { withSome: 'data' }
   }
 ]
 ticket chunk [
   {
     id: '37153ff5-a52b-4a36-8973-b3845211d8f9',
     status: 'error',
     message: "There was an unknown error with the FCM server. See this error's details for more information.",
     messageEnum: 16,
     details: {
       error: 'ProviderError',
       errorCodeEnum: 1,
       fault: 'fcm',
       fcm: [Object]
     }
   }
 ]
 ticket Chunk fcm object access: { error: 'MismatchSenderId' }

The backend:

exports.pushNotification = asyncHandler(async (req, res, next) => {
  const { somePushTokens } = req.body;
  let allToken = [];

  for (let i in somePushTokens) {
    allToken.push(somePushTokens[i].token);
  }

  let expo = new Expo({
    accessToken: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
  });

  let messages = [];
  for (let pushToken of allToken) {
    if (!Expo.isExpoPushToken(pushToken)) {
      console.error(`Push token ${pushToken} is not a valid Expo push token`);
      continue;
    }
    messages.push({
      to: pushToken,
      sound: "default",
      body: "MyApp",
      data: { withSome: "data" },
    });
  }

  let chunks = expo.chunkPushNotifications(messages);
  console.log("chunks", chunks);
  let tickets = [];
  (async () => {
    for (let chunk of chunks) {
      console.log("chunk", chunk);
      try {
        let ticketChunk = await expo.sendPushNotificationsAsync(chunk);
        console.log("ticket chunk", ticketChunk);
        console.log("ticket Chunk fcm object access:", ticketChunk[0].details.fcm);
        tickets.push(...ticketChunk);
      } catch (error) {
        console.error("Error ticket chunk", error);
      }
    }
  })();

  let receiptIds = [];
  for (let ticket of tickets) {
    if (ticket.id) {
      receiptIds.push(ticket.id);
    }
  }

  let receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
  (async () => {
    for (let chunk of receiptIdChunks) {
      try {
        let receipts = await expo.sendPushNotificationsAsync(chunk);
        console.log("recipts: ", receipts);

        for (let receiptId in receipts) {
          let { status, message, details } = receipts[receiptId];
          if (status === "ok") {
            continue;
          } else if (status === "error") {
            console.error(
              `There was an error sending a notification: ${message}`
            );
            console.error(`The error code in status is:  ${details.error}`);
            console.error(`The error fcm in status is":  ${details.fcm}`);
            if (details && details.error && details.fcm) {
              console.error(`The error code is:  ${details.error}`);
              console.error(`The error fcm is":  ${details.fcm}`);
            }
          }
        }
      } catch (error) {
        console.error("The error: ", error);
      }
    }
  })();

  res.status(200).json({
    success: true,
    data: messages,
    msg: "Notification has been send",
  });
});

How can I solve this error?

MismatchSenderId means that your client is registering with a different server than the one you are using to send

refer this doc: https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes

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