title: How can I use FCM from within Firebase function
body: <p>I'm trying to send a FCM from within a firebase function but I'm getting the following error every time:</p>
  <pre><code>await admin.messaging().sendToDevice(registrationToken, message)
  </code></pre>
  <p>I followed the suggestions from <a href="https://stackoverflow.com/questions/42770005/how-can-i-use-firebase-functions-to-send-fcm-to-user">this post, of the same topic</a>, no help.  Pointing to <a href="https://firebase.google.com/docs/admin/setup" rel="nofollow noreferrer">Add the Firebase Admin SDK to your server</a>, another link in the article, isn't very helpful as I am using Firebase Server.</p>
  <p>This article, same, <a href="https://stackoverflow.com/questions/59676856/sending-fcm-using-firebase-functions">Sending FCM using Firebase Functions</a> and I'm using the same code (the FCM code), same error.</p>
  <p>This is the full error:</p>
  <pre><code>Error sending message: { Error: 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: &quot;&lt;HTML&gt;
  &lt;HEAD&gt;
  &lt;TITLE&gt;Unauthorized&lt;/TITLE&gt;
  &lt;/HEAD&gt;
  &lt;BODY BGCOLOR=&quot;#FFFFFF&quot; TEXT=&quot;#000000&quot;&gt;
  &lt;H1&gt;Unauthorized&lt;/H1&gt;
  &lt;H2&gt;Error 401&lt;/H2&gt;
  &lt;/BODY&gt;
  &lt;/HTML&gt;
  &quot;. Status code: 401.
      at FirebaseMessagingError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:43:28)
      at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:89:28)
      at new FirebaseMessagingError (/workspace/node_modules/firebase-admin/lib/utils/error.js:255:16)
      at Object.createFirebaseError (/workspace/node_modules/firebase-admin/lib/messaging/messaging-errors-internal.js:57:12)
      at /workspace/node_modules/firebase-admin/lib/messaging/messaging-api-request-internal.js:78:51
      at process._tickCallback (internal/process/next_tick.js:68:7)
    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: &quot;&lt;HTML&gt;\n&lt;HEAD&gt;\n&lt;TITLE&gt;Unauthorized&lt;/TITLE&gt;\n&lt;/HEAD&gt;\n&lt;BODY BGCOLOR=&quot;#FFFFFF&quot; TEXT=&quot;#000000&quot;&gt;\n&lt;H1&gt;Unauthorized&lt;/H1&gt;\n&lt;H2&gt;Error 401&lt;/H2&gt;\n&lt;/BODY&gt;\n&lt;/HTML&gt;\n&quot;. Status code: 401.' },
    codePrefix: 'messaging' } 
  </code></pre>