代码之家  ›  专栏  ›  技术社区  ›  mystic cola

从Android应用程序调用sendFollowerNotification firebase函数

  •  1
  • mystic cola  · 技术社区  · 7 年前

    所以我意识到,从12.0版开始,你可以直接从Android应用程序调用FireBase函数…对于发送消息的给定示例,这是有意义的:

    private Task<String> addMessage(String text) {
            // Create the arguments to the callable function.
            Map<String, Object> data = new HashMap<>();
            data.put("text", text);
            data.put("push", true);
    
            return mFunctions
                    .getHttpsCallable("addMessage")
                    .call(data)
                    .continueWith(new Continuation<HttpsCallableResult, String>() {
                        @Override
                        public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                            // This continuation runs on either success or failure, but if the task
                            // has failed then getResult() will throw an Exception which will be
                            // propagated down.
                            String result = (String) task.getResult().getData();
                            return result;
                        }
                    });
        }
    

    …向函数发送文本的位置。

    exports.addMessage = functions.https.onCall((data, context) => {
      // [START_EXCLUDE]
      // [START readMessageData]
      // Message text passed from the client.
      const text = data.text;
      // [END readMessageData]
      // [START messageHttpsErrors]
      // Checking attribute.
      if (!(typeof text === 'string') || text.length === 0) {
        // Throwing an HttpsError so that the client gets the error details.
        throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
            'one arguments "text" containing the message text to add.');
      }
      // Checking that the user is authenticated.
      if (!context.auth) {
        // Throwing an HttpsError so that the client gets the error details.
        throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
            'while authenticated.');
      }
    

    但我不确定应该发送什么,比如sendFollowerNotification示例:

    https://github.com/firebase/functions-samples/tree/master/fcm-notifications

    exports.sendFollowerNotification = functions.database.ref('/followers/{followedUid}/{followerUid}')
        .onWrite((change, context) => {
          const followerUid = context.params.followerUid;
          const followedUid = context.params.followedUid;
          // If un-follow we exit the function.
          if (!change.after.val()) {
            return console.log('User ', followerUid, 'un-followed user', followedUid);
          }
    

    我是说…假设用户已登录并具有firebase uid并且在数据库中(当有人登录时,我的应用程序会自动创建firebase用户)…看起来sendFollowerNotification只是从实时数据库中获取所有信息。

    那我该把什么放在下面呢?:

    .call(data)
    

    我如何为我要跟踪的用户检索uid?对于登录并使用应用程序的用户…很明显,我已经有了那个用户的uid、token和其他一切…但我不确定如何为即将被跟踪的用户检索信息…如果这有任何意义的话。

    我在互联网上到处搜索,从来没有发现过一个使用新的Post12.0.0方法的Android应用程序中使用这种特殊类型的函数调用的例子。所以我很想知道正确的语法应该是什么。

    1 回复  |  直到 7 年前
        1
  •  1
  •   mystic cola    7 年前

    好啊!这件事让我很生气,想弄明白…事实证明,您根本不需要调用“sendFollowerNotification”。它所做的就是 侦听对FireBase实时数据库的更改 . 如果对sendFollowerNotification查找的语法进行更改…它会自动发出通知。

    在“sendfollwernotification”中根本没有用于将用户写入实时数据库的调用。我实际上是在登录时处理的:

    private DatabaseReference mDatabase; //up top
    
    mDatabase = FirebaseDatabase.getInstance().getReference(); //somewhere in "onCreate"
    
    final String userId = mAuth.getUid();
    
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    
    mDatabase.child("users").child(userId).child("displayName").setValue(name);
    mDatabase.child("users").child(userId).child("notificationTokens").child(refreshedToken).setValue(true);
    mDatabase.child("users").child(userId).child("photoURL").setValue(avatar);
    

    然后,当一个用户跟踪另一个用户时,我也将其写入实时数据库:

    mDatabase.child("followers").child(user_Id).child(follower_id).setValue(true);
    

    就这样!第二个新的追随者添加到实时数据库…sendFollwerNotification将自动发送通知。你只需要在你的应用程序中设置一个监听程序来接收消息,一旦你的用户点击了一条已经收到的消息,你就可以在哪里重新定向。