1

I need assistance with firestore functions. I'm working on an app that requires notification and i'm using firebase as backend, i'm totally new to the cloud functions features.

So i want send a notification to a user when a document is added to a collection, i've tried setting up some stuff for the functions but it is not working for reasons i dont know, my Node Js is the updated version, firebase tools is updated but i still get errors.

Here is my index.js file and the error message. I appreciate any help.

'use strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.firestore.document('Users/{userId}/Notifications/{notificationId}').onWrite((change, context) =>{

const userId = context.params.userId;
const notificationId = context.params.notificationId;

console.log('The User id is : ', userId);


if(!context.data.val()){

    return console.log('A notification has been deleted from the database');

}

// ref to the parent document
const device_token = admin.firestore.doc('Users/${userId}/Token/${userId}/deviceToken').once('value');

return device_token.then(result => {

    const token_id = result.val();

    const payLoad = {

    notification:{
        title: "Qubbe",
        body: "You have a new comment!",
        icon: "default"
    }

};

return admin.messaging().sendToDevice(token_id, payLoad).then(response => {

        console.log('This is the notification feature');

});

});
device_token.catch(error => {
    console.log(error);
});

});

Errors: Scrrenshot to error in command line

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Geek Dev
  • 51
  • 6
  • 1
    I have exaplained in one of my **[tutorials](https://www.youtube.com/playlist?list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee)** step by step, how you can send **[notifications](https://www.youtube.com/watch?v=6RzB4HXzQyA&t=3s&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=17)** to specific users using `Cloud Firestore` and `Node.js`. You can also take a look at my answer from this **[post](https://stackoverflow.com/questions/48298993/push-notifications-on-content-change/48299840)**. – Alex Mamo Sep 09 '18 at 10:38

2 Answers2

3

Thanks for the replies, the issue is now solved.

I first deleted my old functions, and started afresh then i installed the latest firebase tools globally and update the npm tools as you normally do in a startup. Then i made use of this code for my firestore :

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();



//const firestore = new Firestore();
//const settings = {/* Date... */ timestampsInSnapshots: true};
//firestore.settings(settings);

exports.sendNotification = 
functions.firestore.document('Users/{userId}/Notifications/{notificationId}')
.onWrite((change, context) =>{

const userId = context.params.userId;
const notificationId = context.params.notificationId;

console.log('The User id is : ', userId);
console.log('The Notification id is : ', notificationId);

// ref to the parent document

return admin.firestore().collection("Users").doc(userId).collection("Token").doc(userId).get().then(queryResult => {
    const tokenId = queryResult.data().deviceToken;

    //const toUser = admin.firestore().collection("Users").doc(userId).collection("Notifications").doc(notificationId).get();

        const notificationContent = {
                notification:{
                    title: "/*App name */",
                    body: "You have a new Comment!",
                    icon: "default",
                    click_action: "/*Package */_TARGET_NOTIFICATION"
            }
        };

        return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
            console.log("Notification sent!");
            //admin.firestore().collection("notifications").doc(userEmail).collection("userNotifications").doc(notificationId).delete();
        });

   });

});
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
Geek Dev
  • 51
  • 6
1

Since you are using the following syntax onWrite((change, context)...) I make the assumption that you are using a Cloud Functions version >= 1.0.

With versions >= 1.0 you should initialize firebase-admin without any parameters, as follows:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

See https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase-admin

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121