Send Push Notification from mongodb function using firebase

Hi,

I am trying to send push notification from mongodb function using firebase (fcm).

I have the following code working on node.js, which i am trying to replicate in function:
const admin = require(‘firebase-admin’);
let serviceAccount = require(’./serviceAccountKey.json’);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});

Uploaded all the relevant external dependencies.

Now, the problem is i don’t know how the following code will work:
let serviceAccount = require(’./serviceAccountKey.json’);

this line is looking for external json file (unfortunately, not able to upload it as dependencies)

Any help to make it work would be appreciated.

Thanks

1 Like

hi,

If you set your Sender Id and API Key under Push Notifications -> Config you can use following function to send notifications:

exports = function(deviceToken){
  
  // Construct message
  const message = {
    "to": deviceToken,
    "notification": {
      "title": "Test Title",
      "body": "Test Message"
    }
  };
  
  // Send push notification
  const gcm = context.services.get('gcm');
  const result = gcm.send(message);
  console.log(JSON.stringify(result));
  return result;
};

regards