【问题标题】:Firebase - don't get message with read rulesFirebase - 不要收到带有阅读规则的消息
【发布时间】:2016-09-03 00:57:11
【问题描述】:

我正在尝试使用 Firebase 在 IOS 中建立一个聊天室。当我在 firebase 控制台的模拟器中进行测试时,一切正常,但在 IOS 应用程序中无法正常工作。

我使用的结构是这样的

root/members/room-id/auth.uid/userdata

root/messages/room_id/messages/message

我在 IOS 中创建了一个新房间

- (void)initRoom
{
    NSMutableDictionary *mdata = [[NSMutableDictionary alloc] init];

    FIRUser *user = [FIRAuth auth].currentUser;

    mdata[@"uid"] = user.uid;

    // get the key for the new room

    roomKey = [[_rootRef child:@"messages"] childByAutoId].key;

    if (roomKey != nil) {

        // add auth.uid to member/roomkey

        NSString *memberID = [[[_rootRef child:@"members"] child: roomKey ] child: user.uid ].key;

        [[[[_rootRef child:@"members"] child: roomKey ] child: memberID ]  setValue:mdata]; 

        [self addMemberToRoom];      
    }   
} 

并为该房间添加其他成员(目前使用硬编码的 uid):

-(void) addMemberToRoom{
    NSString *memberID = [[[_rootRef child:@"members"] child: roomKey ] child: @"QLfRoGpoCjWpzira7fljBj8g3EJ3"].key;
    NSMutableDictionary *mdata = [[NSMutableDictionary alloc] init];
    mdata[@"uid"] = @"QLfRoGpoCjWpzira7fljBj8g3EJ3";
    if (memberID !=nil) {
        [[[[_rootRef child:@"members"] child: roomKey ] child: memberID ]  setValue:mdata];
    }

    // start listener

    [self observeMessages];

}

控制台向成员显示此内容: 这是消息:

然后像这样启动监听器:

-(void)observeMessages
{

    _msgHandle = [[[_rootRef child:@"messages"] child: roomKey] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {

        NSDictionary<NSString *, NSString *> *message = snapshot.value;

        NSString *name = message[@"senderId"];
        NSString *text = message[@"message"];

        [self addMessagewithId:name andText:text];

        // animates the receiving of a new message on the view
        [self finishReceivingMessage];
    }];

}

当我设置这样的规则时,将消息添加到数据库并在应用程序中查看它,这一切都很好:

{
  "rules": {
    "members": {
      ".read": "auth !== null",
      ".write": "auth !== null",
     },
     "messages": {
      ".write":"auth !== null",
      ".read": "auth !== null",
          "$room_id": {

      }
    }
  }
}

现在,由于我不希望每个人都能够阅读我设置规则的消息:

{
      "rules": {
        "members": {
           ".read": "auth !== null",
          ".write": "auth !== null",
         },
         "messages": {
         ".write": "auth !== null", 
              "$room_id": {

                  ".read": "root.child('members/'+$room_id+'/'+auth.uid).exists()",  

          }
        }
      }
    }

当我这样做时,IOS 应用程序中的侦听器将不会收到任何呼叫。在 firebase 控制台中使用模拟器检查从应用程序创建的 room_id 和用户,读取标记为 OK。

我的假设是,侦听器在创建新成员之前初始化,但即使延迟初始化也无济于事。

你能帮我指出正确的方向吗?非常感谢!

【问题讨论】:

    标签: ios firebase


    【解决方案1】:

    好的,我找到了一个适用于现在的解决方案。聊天室的观察者是在聊天室在成员列表中可用之前创建的,因此 .read 规则返回 false。

    我的解决方法是在成员列表中设置一个观察者,以便在观察者触发时启动消息观察者。

    这是我的代码:

    -(void) getLastRoom // invoked when new room is added to members node
    {
        FIRUser *user = [FIRAuth auth].currentUser;
        NSString *uid = user.uid;
        _msgHandle = [[[[_rootRef child:@"members"] child: roomKey] queryEqualToValue: uid] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
    
            NSString *chatRoom = snapshot.key; //-> returns the roomkey of the chatroom
    
            if ([chatRoom isEqualToString:roomKey]){ 
                [self observeMessages];
            }
    
        }];
    }
    

    希望它可以帮助某人。

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 2014-12-19
      • 2020-09-10
      • 2019-05-27
      • 2018-05-15
      • 1970-01-01
      相关资源
      最近更新 更多