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

时间:2018-07-08 20:37:25

标签: android firebase firebase-realtime-database

因此我意识到,从12.0版开始,您可以直接从Android应用程序调用Firebase Functions ...对于给定的发送消息示例,这很有意义:

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,令牌和其他所有信息……但是我不确定如何为将要关注的用户检索该信息……如果有意义的话。

我已经在整个Internet上进行了搜索,但是从未找到使用新的12.0.0方法在android应用程序中使用过这种特殊类型的函数调用的示例。所以我很好奇应该知道正确的语法。

1 个答案:

答案 0 :(得分:1)

好!这真的激怒了我试图弄清楚的事实……事实证明,您根本不需要调用“ 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将自动发送通知。您只需要在应用程序中设置一个侦听器来接收消息,并在用户点击收到的消息并完成操作后将其重定向到用户。