发送 fcm http v1 推送通知时出现错误 { code: 403,message: 'SenderId mismatch', status: 'PERMISSION_DENIED',details: [ [Object] ] }

问题描述 投票:0回答:1

我正在将 aws sns 旧版 Firebase Cloud Messaging (FCM) API 升级到 FCM http v1 api。为此,在我的 lambda 函数(在 Nodejs 中)中添加了以下代码。

const admin = require('firebase-admin');
const { GoogleAuth } = require('google-auth-library');
const axios = require('axios');
const serviceAccount = require('../../service-account.json');
const auth = new GoogleAuth({
  credentials: serviceAccount,
  scopes: ['https://www.googleapis.com/auth/firebase.messaging']
});
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)


});

async function getAccessToken() {
      const client = await auth.getClient();
      const token = await client.getAccessToken();
      console.log("token ",token.token)
      return token.token;
}
    
async function sendFcmMessage(message) {
      const token = await getAccessToken();
    
      try {
      const response = await axios.post(
        'https://fcm.googleapis.com/v1/projects/projct_id/messages:send',
        {
          message: message
        },
        {
          headers: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        }
      );
    
      console.log('Successfully sent message:', response.data);
      } catch (error) {
        console.error('Error sending message:', error.response ? error.response.data : error.message);
      }
}


const sentmessage = {
        token: deviceToken,
        notification: {
          title: 'Test Notification',
          body: realDeviceName + ' ' + txtLevel + ': ' + txtValue, // "body" can be used as alias, is converted to "message"
        }
    };

sendFcmMessage(sentmessage).catch(console.error);

使用上面的代码我得到

{ code: 403,message: 'SenderId mismatch', status: 'PERMISSION_DENIED',details: [ [Object] ] }

如果有人知道此错误,请告诉我。谢谢。

node.js amazon-web-services firebase http firebase-cloud-messaging
1个回答
0
投票

这是更新后的代码。

async function sendFcmMessage(message) {
  const token = await getAccessToken();

  try {
    const data = {
      message: {
        token: token,
        notification: {
          title: title,
          body: body,
        }
      }
    };

    const config = {
      method: 'post',
      url: 'https://fcm.googleapis.com/v1/projects/abillion-analytics/messages:send',
      headers: {
        Authorization: `Bearer ${token}`,
        'Content-Type': 'application/json',
      },
      data: data,
    };
    const response = await axios(config);
    console.log(response.data);
    console.log('App Notification Sent');
  console.log('Successfully sent message:', response.data);
  } catch (error) {
    console.error('Error sending message:', error.response ? error.response.data : error.message);
  }
}


sendFcmMessage().catch(console.error);

通过确保正确的 axios 配置来解决错误。

© www.soinside.com 2019 - 2024. All rights reserved.