如何确定具有特定令牌的设备是否在线与Firebase的Android? [英] How to determine if a device with a specific token is online with firebase android?

查看:110
本文介绍了如何确定具有特定令牌的设备是否在线与Firebase的Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

div>

当应用程序启动时,您可以使用 https:// www .firebase.com / docs / web / api / ondisconnect / remove.html ,以确保在客户端脱机时删除特定的数据。然后,你可以在别的地方看到数据是否在线(仍然在线)或不在线(现在离线)。

这个页面 https://www.firebase.com/docs/web/api/ondisconnect/ 声明如下



客户端与Firebase服务器断开连接时,onDisconnect类允许您写入或清除数据。这些更新发生
,无论您的客户端是否干净地断开连接,所以即使连接断开或客户端
崩溃,您也可以依靠
来清理数据。



onDisconnect类最常用于管理
应用程序中的状态,其中检测有多少客户端连接
以及何时断开其他客户端是非常有用的。查看离线功能
了解更多信息。

假设您的数据库中有在线用户列表。就像这样:

pre $ code $ online $ user $ {
user1:true,$ b $ user23:true,
user100:true
}

该例子说明用户user1,user23和user100在线。比方说,user23联机,你想要的firebase数据库,以反映该用户是否在线,这样做:

  var disconnectRef = new Firebase('https://< YOUR-FIREBASE-APP> .firebaseio-demo.com / onlineUsers / user23'); 
disconnectRef.onDisconnect()。remove();

上面的代码在执行时不会删除任何内容。它只是告诉Firebase服务器在客户端脱机时移除onlineUsers / user23。



只要客户端(例如Android应用/设备)脱机,Firebase服务器将确保新状态将如下所示:

  onlineUsers:{
user1:true,
user100:true





正如你所看到的,user23不再出现在列表中。

I was wondering how it is possible to determine if a device with token 'A' is online with another android device using firebase framework?

解决方案

When app starts, you can use https://www.firebase.com/docs/web/api/ondisconnect/remove.html to make sure that a specific data is removed when client comes offline. Then you can, somewhere else, see if that data is there (still online) or not (now offline).

This page https://www.firebase.com/docs/web/api/ondisconnect/ states the following

The onDisconnect class allows you to write or clear data when your client disconnects from the Firebase servers. These updates occur whether your client disconnects cleanly or not, so you can rely on them to clean up data even if a connection is dropped or a client crashes.

The onDisconnect class is most commonly used to manage presence in applications where it is useful to detect how many clients are connected and when other clients disconnect. See Offline Capabilities for more information.

Let´s say you have a list of online users in your db. Something like this:

onlineUsers: {
  user1: true,
  user23: true,
  user100: true
}

The example illustrates that users user1, user23 and user100 are online. Let´s say user23 comes online and you want the firebase db to reflect if that user is online or not, do this:

var disconnectRef = new Firebase('https://<YOUR-FIREBASE-APP>.firebaseio-demo.com/onlineUsers/user23');
disconnectRef.onDisconnect().remove();

The above code does not remove anything at execution. It just tells the Firebase server to remove onlineUsers/user23 when client comes offline.

As soon as that client (say Android app/device) comes offline, the Firebase servers will make sure that the new state will be like this:

onlineUsers: {
  user1: true,
  user100: true
}

As you can see, user23 is no longer present in the list.

这篇关于如何确定具有特定令牌的设备是否在线与Firebase的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆