Debugging push notification issues requires skill and effort. It's very helpful to be able to send targeted push notification to designated device for testing purposes.
In this blog, I will document how to achieve it.
A heads-up, the main hurdle lies in retrieveing an access token for authentication. Compared to the legacy API that used a server key, the new one requires a bit of extra work to get the API to function properly (for good reason).
Endpoint:
POST https://fcm.googleapis.com/v1/projects/{YOUR-PROJECT-ID}/messages:send
Get {YOUR-PROJECT-ID}
from Firebase console, Project Settings > General
Body:
{
"message":{
"token": "FCM token on device",
"notification":{
"body":"This is an FCM notification message!",
"title":"FCM Message"
}
}
}
Headers:
Content-Type: application/json
Authorization: Bearer {accessToken}
To retrieve accessToken
, you need:
gcloud CLI
Private key file in JSON format (From Project Settings > Service Accounts
.)
Using GOOGLE_APPLICATION_CREDENTIALS
environment variable:
GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/secret.json /path/to/your/google-cloud-sdk/bin/gcloud auth application-default print-access-token
An access token will be printed to the terminal. Use it for the Authorization
header.
Thank you for reading! Cheers!