【问题标题】:Push Notification from Firestore来自 Firestore 的推送通知
【发布时间】:2019-01-18 01:00:06
【问题描述】:

当我的 Cloud Firestore 数据库中的数据发生变化时,我需要发送通知。我有这个字段

我需要获取所有用户令牌并发送推送通知。我有一个代码,但是如果我知道用户名,这只会给我一个令牌,这是我的代码:

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

exports.useWildcard = functions.firestore
    .document('notification/{id}')
    .onWrite((change, context) => {

        const payload = {
            notification: {
                title: 'Message from Cloud',
                body: 'This is your body',
                badge: '1',
                sound: 'default'
            }
        };

        admin.firestore().collection('notification').doc('fcm-token').get().then(doc => {
            console.log("Token: " + doc.data().user1.token);
        });

    });

【问题讨论】:

  • 我希望您不打算将所有用户的令牌存储在一个文档中。那根本不会扩展。您可能需要考虑如何构建数据以使您的策略发挥作用。

标签: javascript firebase google-cloud-firestore google-cloud-functions firebase-admin


【解决方案1】:

遍历文档中的所有用户:

admin.firestore().collection('notification').doc('fcm-token').get().then(doc => {
  let data = doc.data();
  Object.keys(data).forEach((user) {
    console.log("Token: " + data[user].token);
  });
});

但正如 Doug 所说:将所有用户的令牌存储在单个文档中势必会成为可扩展性问题。

【讨论】:

  • 这告诉我一个错误 TypeError: Object.key is not a function at admin.firestore.collection.doc.get.then.doc (/user_code/index.js:22:20) at process ._tickDomainCallback (internal/process/next_tick.js:135:7)
  • 糟糕......这是一个错字:Object.keys()。见developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
猜你喜欢
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 2015-07-24
  • 1970-01-01
  • 2023-03-06
  • 2018-06-21
相关资源
最近更新 更多