如何在云功能中传递动态参考?

时间:2017-12-19 06:37:36

标签: node.js firebase firebase-cloud-messaging google-cloud-functions

我想使用云功能为特定用户发送推送通知。

这是我的代码:

const payload = {
    notification: {
        title: 'Test message',
        body: 'Hope you are fine',
        badge: '1',
        sound: 'default',
    }
};

但是,如何动态地进行,即我想为特定用户发送推送通知,此代码将通知发送给所有已注册的用户。因此,帮助我将静态id(db引用)更改为dynamic(db reference),以及如何在此代码行中传递不同的通知内容:

android:layout_gravity="center"

有人在那指导我吗?

1 个答案:

答案 0 :(得分:1)

我是动态实现的。希望它可以帮到某人。

这是我的代码:

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

exports.sendPushNotification = functions.database.ref('/Users/{id}').onWrite(event => {

var eventSnapshot = event.data;
    var particularUser = eventSnapshot.child('City');
    if (particularUser.changed()) {
      const payload = {
              notification: {
                  title: 'Test message',
                  body: 'Hope you are fine',
                  badge: '1',
                  sound: 'default',
              }
          };

          var getToken = eventSnapshot.child('fcmToken');

                  const token = Object.keys(getToken.val());
                  return admin.messaging().sendToDevice(token, payload).then(response => {

                  });

    }


});

如果要更改通知内容,可以从应用端更新后从数据库中获取。