简体   繁体   中英

sending notification with firebase cloud messaging

I'm trying to send notification to users with firebase cloud messaging when they are being sent friend request in my app using the below function which i deployed to my firebase functions

 'use strict' const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notifications_id}').onWrite(event => { const user_id = event.params.user_id; const notifications = event.params.notifications; console.log('This User Id is :', user_id); }); 

but when it trys to send the notification, i get the following error in my functions logs

  sendNotification TypeError: Cannot read property 'user_id' of undefined at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:8:30) at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20) at /var/tmp/worker/worker.js:758:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7) 

you have to add context and get the parameter from there. Try:

            ..'use strict'

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

            exports.sendNotification = functions.database
              .ref('/Notifications/{user_id}/{notifications_id}')
              .onWrite((event, context) => {

                   const user_id = context.params.user_id;
                   const notifications = context.params.notifications;

                  console.log('This User Id is :', user_id);
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM