AWS Lambda with Firebase Messaging – Long posts

AWS Lambda with Firebase Messaging

Robert
@pnut developerd
Notes on wiki.pnut.io
I take being emperor seriously but I feel like I'm the only one.

@33MHz on Pnut

Technology mash:

  • Firebase Cloud Messaging
  • AWS Lambda cron
  • Serverless

I found the least haphazard way to authenticate was to just take the variables that are needed from the google-application-credentials.json file that Firebase gives me, and set them up as Lambda environment variables.

Serverless Variables

I added the JSON file to .gitignore, and let Serverless extract the variables from the file:

environment:
    FIREBASE_PROJECT_ID: ${file(./google-application-credentials.json):project_id}
    FIREBASE_CLIENT_EMAIL: ${file(./google-application-credentials.json):client_email}
    FIREBASE_PRIVATE_KEY: ${file(./google-application-credentials.json):private_key}

Firebase Initialization

Now I can recreate the cert object:

const firebase = require("firebase-admin")

firebase.initializeApp({
    credential: firebase.credential.cert({
        projectId: process.env.FIREBASE_PROJECT_ID,
        clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
        privateKey: process.env.FIREBASE_PRIVATE_KEY,
    }),
})

export { firebase }

I have this within its own service file, so it's easily mocked and imported variously.

Then I'm free to call firebase.messaging().sendToDevice(id, payload, options) wherever needed.

Filed Under: "dev"
Share Link

372 approximate views

Activity: 0 Replies, 0 Reposts, 0 Bookmarks

Discussion

View on Beta

Log in to comment.