firebase / firebase-tools

The Firebase Command Line Tools
MIT License
3.99k stars 920 forks source link

Firebase cloud messaging topics not functional in emulator #4222

Closed vidicunt closed 2 years ago

vidicunt commented 2 years ago

Hello! I'm trying to test FCM using firebase emulator, however it isn't functional. For instance:

const message = {
  data: {
    info: "myInfo"
    },
  topic: "myTopic"
};

await admin.messaging().send(message)

This shows an error in the console:


>  var _this = _super.call(this, errorInfo.message) || this;
>                             ^
>
>  FirebaseMessagingError: An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions. Raw server response: "<HTML>
>  <HEAD>
>  <TITLE>Unauthorized</TITLE>
>  </HEAD>
>  <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
>  <H1>Unauthorized</H1>
>  <H2>Error 401</H2>
>  </BODY>
>  </HTML>
>  ". Status code: 401.
>      at FirebaseMessagingError.FirebaseError [as constructor] (G:\Android_Development\Active_Android_Development\_firebase\functions\node_modules\firebase-admin\lib\utils\error.js:44:28)
>      at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (G:\Android_Development\Active_Android_Development\_firebase\functions\node_modules\firebase-admin\lib\utils\error.js:90:28)
>      at new FirebaseMessagingError (G:\Android_Development\Active_Android_Development\_firebase\functions\node_modules\firebase-admin\lib\utils\error.js:279:16)
>      at Object.createFirebaseError (G:\Android_Development\Active_Android_Development\_firebase\functions\node_modules\firebase-admin\lib\messaging\messaging-errors-internal.js:57:12)
>      at G:\Android_Development\Active_Android_Development\_firebase\functions\node_modules\firebase-admin\lib\messaging\messaging-api-request-internal.js:79:51
>      at processTicksAndRejections (node:internal/process/task_queues:96:5)
>      at async subscribeFcmAdmin (G:\Android_Development\Active_Android_Development\_firebase\functions\lib\adminModule.js:373:9) {
>    errorInfo: {
>      code: 'messaging/authentication-error',
>      message: 'An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions. Raw server response: "<HTML>\n' +
>        '<HEAD>\n' +
>        '<TITLE>Unauthorized</TITLE>\n' +
>        '</HEAD>\n' +
>        '<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n' +
>        '<H1>Unauthorized</H1>\n' +
>        '<H2>Error 401</H2>\n' +
>        '</BODY>\n' +
>        '</HTML>\n' +
>        '". Status code: 401.'
>    },
>    codePrefix: 'messaging'
>  }

Note, this only happens for FCM topics. I am using the emulators on a Windows 10 machine.

yuchenshi commented 2 years ago

The Emulator Suite does not support FCM (yet). Is it possible you're getting the error because the code is talking to the production server? If a FCM emulator is what you want, please open a feature request instead.

angelzarazua commented 2 years ago

Thanks for the answer, i spent 3 days trying to figure this out.

shazlycode commented 2 years ago

Go to google cloud then go to API and Services then Enabled API and Services then turn on the cloud messaging Turn on Firebase cloud messaging API.