firebasepush-notificationfirebase-cloud-messaging

How to test send Firebase silent push notifications?


I don't have experience using Firebase, and I'm trying to test send a silent push notification through FCM to an Android app I'm developing. It seems I can only send regular notifications from the Firebase admin (which I've been able to do), but not the silent ones I'm trying to test.

I'm trying to use cURL to hit FCM's HTTP V1 API this way:

curl -H "Content-type: application/json" -H "Authorization: Bearer <OAuth id>" -X POST -d '{ "data":{ "foo":"1", "bar":"2" }, "to":"<token>", "android":{ "priority":"high" }}' https://fcm.googleapis.com/v1/projects/{project name}/messages:send

token: FCM registration token that I get through the android app on my test device, seems to be correct.
OAuth id: "OAuth 2 Client ID" that I got from https://console.cloud.google.com/iam-admin/serviceaccounts?project={project name}&supportedpurview=project, which I'm not sure is the one I should use.

enter image description here


I'm getting UNAUTHENTICATED as a response.

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

Is there a straight-forward way to test silent push notifications, or is it supposed to be this convoluted? If so, what am I missing here?


Solution

  • Based on the response itself, the request to send FCM notification contains an invalid OAuth 2.0 access token. To generate a valid OAuth 2.0 access token, kindly follow the instructions given in this documentation.

    For testing purposes, you can try to generate an OAuth 2.0 access token using the options below:

    1. Using Google Developers OAuth 2.0 Playground

    2. Using Firebase Cloud Messaging Node.js Quickstart Project

      • Install Firebase Admin SDK and Node.js
      • Download the quickstart project and follow the guide in README.md file
      • Update index.js and log the access token
      • Copy the access token and use it in the request

    Note that the usual prefix of access token is ya29.

    Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA