Home > Software design >  Cloud functions with Firebase Realtime Database not triggering onCreate and not sending FCM notifica
Cloud functions with Firebase Realtime Database not triggering onCreate and not sending FCM notifica

Time:11-22

I'm trying to automatically send notification per onCreate event of RealtimeDatabase, however the function never triggers and never logs anything.

code:

// The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

exports.notificationOnArticleCreate = functions.database.ref().onCreate((snapshot, context) => {
  const payload = {
    notification: {
      title: 'cloud function demo',
      body: 'this is a test notification'
    }
  };
  
  admin.messaging().send(payload).then((response) => {
      console.log('Successfully sent message:', response);
      return {success: true};
    }).catch((error) => {
      return {error: error.code};
    });

    console.log('test test');
})

CodePudding user response:

I'm surprised this even compiles/deployed: functions.database.ref().onCreate.

You'll want to indicate the path where the node is created, e.g.

functions.database.ref("/messages/{message}").onCreate(...
  • Related