admin管理员组

文章数量:1122846

I am trying to integrate the Firebase Admin SDK into my backend Node.js service to send messages based on the official FCM documentation. However, every time I start the service, it gets stuck at the admin.messaging().send(message) step without any response (no success output and no caught exceptions), then got this:

/Users/dbw/WebstormProjects/fcm_test/node_modules/firebase-admin/lib/utils/api-request.js:997
                throw new error_1.FirebaseAppError(error_1.AppErrorCodes.NETWORK_ERROR, `Error while making requests: ${error}`);
                ^

FirebaseAppError: Error while making requests: Error: connect ETIMEDOUT 2001:4860:4802:36::39:443
    at ClientHttp2Session.<anonymous> (/Users/dbw/WebstormProjects/fcm_test/node_modules/firebase-admin/lib/utils/api-request.js:997:23)
    at ClientHttp2Session.emit (node:events:513:28)
    at emitClose (node:internal/http2/core:1085:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errorInfo: {
    code: 'app/network-error',
    message: 'Error while making requests: Error: connect ETIMEDOUT 2001:4860:4802:36::39:443'
  },
  codePrefix: 'app'
}

I fixed this problem by adding httpProxy(I'm in China, need a proxy to visit the International Internet), referred this issue:github/firebase/firebase-admin-node/issues/1724

But same problem appears when I use admin.messaging().sendEach([message]) and admin.messaging().sendEachForMulticast(multiMessage). However, it worked fine in my Java project without proxy.

Could anyone tell me is there any difference in NodeJs And Java environment? And how should I do to resolve the problem in NodeJs?

Here is Typescript code using admin.messaging().sendEachForMulticast(multiMessage):

import * as admin from 'firebase-admin';
import { credential } from 'firebase-admin';
import { HttpsProxyAgent } from 'https-proxy-agent';
const credentialsPath = '/Users/dbw/Downloads/mars-messaging-test-firebase-adminsdk-i2mzh-362b2803d1.json';
const credentials = require(credentialsPath);
const httpAgent = new HttpsProxyAgent('http://127.0.0.1:7890/');
const app = admin.initializeApp({
    credential: credential.cert(credentials),
    httpAgent
});

const registrationToken =
    'my token';
const message = {
    "notification":{
        "title":"I am a title",
        "body":"I am a body"
    },
    token: registrationToken
};

const multiMessage = {
    data: {
        score: '850',
        time: '2:45'
    },
    tokens: [registrationToken]
};

const main = async () => {
    try {
        const result = await app.messaging().sendEachForMulticast(multiMessage);
        console.log('result: ' + result);
    } catch (err) {
        console.log('err: ' + err);
    }
};

本文标签: nodejsFirebase admin SDK in NodeJs FirebaseAppError connect ETIMEDOUTStack Overflow