Back to blog
Oct 12, 2024
3 min read

Send Test Firebase Data Message

How to send a test Firebase data message

Prerequisites

Implement the SDK of your choice in your project
Follow the up to date instructions described here

Why you need it?

You need it in order to receive a notification and print the FCM_TOKEN

FCM_TOKEN (expand for instructions)
This token is what Firebase uses to identify the recipient of a message, which is why a unique token is generated for each device. The best way to retrieve this token is by logging it to the console once it's generated by the Firebase Cloud Messaging (FCM) SDK you're using. In my case, it was as simple as printing it when it became available.
String? fcmToken = await FirebaseMessaging.instance.getToken();
print(fcmToken);
PROJECT_NUMBER (expand for instructions)
  1. Go to Firebase Console
  2. Select your project
  3. Then Project Overview > Project settings
  4. In General tab you look for Project number
PRIVATE_TOKEN (expand for instructions)

Regenerate and Download the Service Account Key

You can skip this step if you already generated the file

  1. Go to the Firebase Console
  2. Select Your Project: Choose the project for which you want to send messages.
3. Navigate to Project Settings: Click on the gear icon next to "Project Overview" and select "Project settings". gcloud auth application-default print-access-token
  1. Service Accounts: Go to the “Service accounts” tab.
  2. Generate a New Private Key: Click on “Generate new private key” button. This will download a new JSON file containing your service account key.
  3. Add the creds using the following command.
export GOOGLE_APPLICATION_CREDENTIALS="path/to/service-account-file.json"

Generate PRIVATE_TOKEN using gcloud

  1. Install clould from here
  2. Generate the token (it could prompt you to log in with your firebase account)
gcloud auth application-default print-access-token
Expected for example gcloud auth application-default print-access-token

Keep in mind that In some short time it can expire and you can see a 401 Unauthorized error But don’t worry you don’t need to go through the whole process again. If this happens you just need to execute gcloud auth application-default print-access-token again

Step 1

Copy the CURL

curl --request POST \\
  --url <https://fcm.googleapis.com/v1/projects/{PROJECT_NUMBER}/messages:send> \\
  --header 'Authorization: Bearer {PRIVATE_TOKEN}' \\
  --header 'Content-Type: application/json; UTF-8' \\
  --data '{
 "message": {
  "token": "{FCM_TOKEN}",
  "data": {
   "title": "Test Data Message",
   "message_body": "This is a test data message",
   "customKey1": "value1",
   "customKey2": "value2"
  }
 }
}'

⚠ Remember to change the creds marked between curly braces

Step 2

Open Insomnia or Postman

Step 3

Paste the CURL into Insomnia or Postman and send it