【问题标题】:firebase functions push Notifications to specific userfirebase 功能将通知推送给特定用户
【发布时间】:2017-11-14 11:53:11
【问题描述】:

当其他用户喜欢他/她的帖子时,我想通过 firebase 功能向发布帖子的用户发送推送通知。

i want to get the highlighted user-id in the image to get fcm token of this user id stored in other tree.

下面是我的firebase函数代码。

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


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


exports.sendNotification = functions.database.ref('/user-posts/{userID}/{pushId}/stars/')
        .onWrite(event=> {
            var request = event.data.val();

            console.log("request",request);
            console.log("key",Object.keys(request)[0]);
            var key = Object.keys(request)[0];
            var token;




            const payload = {
          notification: {
            title: 'You have a new follower!',
            body: 'is now following you.'
          }
        };



             const getDeviceTokensPromise = admin.database()
             .ref(`/users-notifications/${key}`)
             .once('value').then(function(snapshot) {
                    console.log("val",snapshot.val());
                        token= snapshot.val();


                        admin.messaging().sendToDevice(token,payload)
            .then(response=>{
                console.log("Successfully sent message:", response);

            })
            .catch(function(error){
                console.log("error sending message",error);
            })
        })

                    }, function(error) {
                        // The Promise was rejected.
                    console.error(error);
                    });

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    您可以使用事件引用的父属性向后遍历 DB 树。

    userID = event.data.ref.parent.parent.parent.key
    

    event.data.ref 的父级是“星星” “stars”的父级是您的 pushID pushID 的父级是 userID

    【讨论】:

      【解决方案2】:

      尝试使用event.params.userID从uri中提取它

      【讨论】:

      • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
      • 这是我提出的解决方案。我错误地把它变成了一个问题。
      猜你喜欢
      • 1970-01-01
      • 2021-03-18
      • 2019-08-23
      • 2019-12-18
      • 2021-04-12
      • 2021-03-03
      • 2018-06-01
      • 2018-01-22
      • 2020-12-04
      相关资源
      最近更新 更多