Firebase 函数 - 创建新文档时推送通知

The*_*eal 3 firebase firebase-realtime-database google-cloud-functions google-cloud-firestore

我有以下 Firestore 数据库结构:

users
  $USER_ID
     notifications
       $DOC1
       $DOC2
       $DOC3
Run Code Online (Sandbox Code Playgroud)

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

它应该是这样的,但我不知道每个 $UID 有什么方法:

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

我如何使用 Firebase 函数来做到这一点?如果没有办法,有什么解决方法的建议吗?

Ren*_*nec 6

您应该使用以下代码来触发您的云函数:

    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
        });
Run Code Online (Sandbox Code Playgroud)

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