Firebase功能-创建新文档时推送通知

时间:2018-07-24 18:29:54

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

我具有以下Firestore数据库结构:

users
  $USER_ID
     notifications
       $DOC1
       $DOC2
       $DOC3

我想在用户通知集合中创建文档时推送新的通知。

应该是这样,但是我不知道每个$ UID都可以这样做:

exports.newSubscriberNotification = functions.firestore
  .document('users/$UID/notifications')
  .onCreate(async event => {

如何使用Firebase功能来做到这一点?如果无法解决,请问有什么建议吗?

1 个答案:

答案 0 :(得分:3)

您应使用以下代码触发您的Cloud Function:

exports.newSubscriberNotification = functions.firestore
    .document('users/{userId}/notifications/{docId}')
    .onCreate((snap, context) => {

      //You get the values of the newly created doc as follows:
      const newValue = snap.data();
      console.log(newValue);

      //You get the parameters as follows:
      const userId = context.params.userId;
      //console.log(userId);

      const docId = context.params.docId;
      //console.log(docId);

      // You perform here the notification sending
    });

有关发送通知的代码,请查看以下官方Firebase Cloud Function示例:https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js