enemy123
enemy123

Reputation: 43

Realtime Database onWrite triggers in Cloud Functions has null val() data

I am trying to implement push notifications trigger using cloud functions in firebase but each time I try the val function returns null. It is not recognizing the pushID, I implemented database from android using push() method. This is my database structure enter image description here

And this is my code for push notifications whenever a Post is created.

  //import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


// Listens for new messages added to messages/:pushId
exports.pushNotification = functions.database.ref('/Posts/Posts/{pushId}').onWrite( event => {

    console.log('Push notification event triggered');

    //  Grab the current value of what was written to the Realtime Database.
    var valueObject = event.data.val();

    // if(valueObject.photoUrl != null) {
    //   valueObject.photoUrl= "Sent you a photo!";
    // }

  // Create a notification
    const payload = {
        notification: {
            title:valueObject.tittle,
            body: valueObject.details,
            sound: "default"
        },
    };

  //Create an options object that contains the time to live for the notification and the priority
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24
    };


    return admin.messaging().sendToTopic("pushNotifications", payload, options);
});

This is error in console of cloud functions

enter image description here

Edited after using OnCreate:-

   exports.pushNotification = functions.database.ref('/Posts/Posts/{pushid}').onCreate((snap, context) => {

const original = snapshot.val();
    console.log('Push notification event triggered');

    //  Grab the current value of what was written to the Realtime Database.
    // var valueObject = event.data.val();
 var valueObject = snap.val();
    // if(valueObject.photoUrl != null) {
    //   valueObject.photoUrl= "Sent you a photo!";
    // }

  // Create a notification
    const payload = {
        notification: {
            title:valueObject.tittle,
            body: valueObject.details,
            sound: "default"
        },
    };

Upvotes: 0

Views: 787

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317342

It looks like you haven't adapted your code to the new Functions 1.0 SDK. The differences are detailed here: https://firebase.google.com/docs/functions/beta-v1-diff

As you can see from that doc in the Realtime Database section, onWrite triggers now give you a Change object with before and after properties that you use to get the value of the database location before or after the update.

Also consider if you actually want an onCreate trigger instead, which is more straightforward to deal with, and only triggers once when data at the matching location is newly created.

Upvotes: 2

Related Questions