Skip to content

Instantly share code, notes, and snippets.

@Debdutta-Panda
Created July 22, 2024 10:07
Show Gist options
  • Save Debdutta-Panda/a5d9da0cfdcf4cd30b0466ea28f06b6c to your computer and use it in GitHub Desktop.
Save Debdutta-Panda/a5d9da0cfdcf4cd30b0466ea28f06b6c to your computer and use it in GitHub Desktop.
Firebase new v1 api to send push notification

Firebase new v1 api to send push notification

To send push notification we need

  1. new url
  2. access token
  3. new payload structure
var { google } = require("googleapis");
var MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
var SCOPES = [MESSAGING_SCOPE];
function getAccessToken() {
return new Promise(function (resolve, reject) {
var jwtClient = new google.auth.JWT(
"put.email@from.privatekey.json",
null,
"put_your_private_key_from_json",
SCOPES,
null
);
jwtClient.authorize(function (err, tokens) {
if (err) {
reject(err);
return;
}
resolve(tokens.access_token);
});
});
}
getAccessToken().then(function (result) {
console.log(result);// copy this access token
});
  1. create new firebase project or ensure it exists
  2. go to service accounts of this project in gcp
  3. select the service account
  4. go to keys
  5. create a new key
  6. one json file will be downloaded
curl --location 'https://fcm.googleapis.com/v1/projects/jaya-sales-19fe4/messages:send' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer that_access_token' \
--data '{
"message":{
"token":"device-access-token",
"data": {
"title": "Hello",
"body": "World",
"channelId": "channelId1",
"channelDescription": "channelDescription1",
"destination": "https://www.google.com",
"notificationId": "123"
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment