从一个用户向另一个用户推送通知

时间:2018-07-21 14:14:25

标签: firebase push-notification notifications firebase-cloud-messaging messaging

我无法从一个用户向另一个用户发送带有用户名的通知  函数给出了这个“ const from_sender_id = fromUserResult.val()。from;我在代码中使用的错误

'use strict'

 const functions = require('firebase-functions');

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


 exports.sendNotification = 
  functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
    .onWrite((change, context) => {
        const receiver_id = context.params.receiver_id;
          const notification_id = context.params.notification_id;
          console.log('We have a Notifications to send to :', 
     receiver_id);
           if (!change.after.val()) {
            return console.log('A Notifications has been deleted from the 
  database', notification_id);
    }



    const sender_id = 
    admin.database().ref(`/Notifications/{receiver_id}/{notification_id}`)
     .once('value');
     return sender_id.then(fromUserResult =>{
       const from_sender_id = fromUserResult.val().from;
       console.log('You Have A Notification From :', from_sender_id);

       const senderUserQuery = 
      admin.database().ref(`/Users/{from_sender_id}/user_name`)
      .once('value');
       return senderUserQuery.then(senderUserNameResult =>{
        const senderUserName = senderUserNameResult.val();


                 const deviceToken = 
       admin.database().ref(`/Users/${receiver_id}/device_token`)
                .once('value');
                 return deviceToken.then(result => {
                        const token_id = result.val();
           const payload = {
           notification: {
                     title: 'New Friend Request',
                body: `${senderUserName} has Sent You a Friend Request`,
                     icon: 'default'
                       }
               };
           return admin.messaging().sendToDevice(token_id, payload)
                .then(response => {
                console.log('This was the notification feature.');
          });
      });
    });
 });
});

this is the pic of firebase function

0 个答案:

没有答案