Skip to content

Instantly share code, notes, and snippets.

@fischerscode
Last active October 26, 2020 15:20
Show Gist options
  • Save fischerscode/0a3c64fac86b54dc3ea330439eb62467 to your computer and use it in GitHub Desktop.
Save fischerscode/0a3c64fac86b54dc3ea330439eb62467 to your computer and use it in GitHub Desktop.
A short tutorial about creating a custom service account and role inside of the Goolge Cloud Platform for sending a Firebase Cloud Message (FCM) from a nodejs project.

Sending a firebase cloud message from a nodejs project using a custom service acccount

Creating the Service Account

  1. open the Google Cloud Platform
  2. select your project
  3. go to IAM & Admin
  4. go to Roles
  5. press CREATE ROLE
  6. enter a title, description and ID
  7. edit the Role launche stage as you please
  8. press ADD PERMISSIONS
  9. enter cloudmessaging.messages.create in the Filter table field
  10. select cloudmessaging.messages.create
  11. press ADD
  12. press CREATE
  13. go to Service Accounts
  14. press CREATE SERVICE ACCOUNT
  15. enter a name as well as a description
  16. press CREATE
  17. select your new role
  18. press DONE
  19. press Create key at the actions of your new service account
  20. select JSON and press create
  21. download the .json file

Sending a cloud message from nodejs

  1. create an ENV variable GOOGLE_APPLICATION_CREDENTIALS pointing to your downloaded file
  2. send a push notification from your nodejs project:
const admin = require("firebase-admin")

admin.initializeApp({
    credential: admin.credential.applicationDefault(),
});

let message = {
    token: "THE CLIENT TOKEN",
    notification: {
        title: "TEST MESSAGE",
        body: "This is a test message.",
    },
};

admin.messaging().send(message)
    .then((response) => {
        console.log('Successfully sent message:', response);
    })
    .catch((error) => {
        console.log('Error sending message:', error);
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment