如何知道应用程序用户是否离线? [英] How to know if app user is offline?
问题描述
我的聊天应用程序具有以下方法:
- 用户登录App时,其状态更改为在线". 状态= 1"
- 当用户退出App时,其状态将更改为最近可用"的脱机状态. 状态= 0"
想法是,当登录的用户断开与Internet的连接时,其状态仍为1.此外,如果用户删除了该应用,其状态仍为1.
在删除/删除应用程序后,是否有代码或方法可以检测用户上次上网的时间?许多删除了该应用程序的用户仍然具有在线"状态.
在Activity,onStart()
和onResume()
内,您可以通过以下方式将在线状态消息发送到服务器:
-
1 (表示在线)
分别为 -
0 (表示离线)
但是问题是如果应用程序出现在前台或互联网停止运行,或者用户卸载了该应用程序,将会发生什么?
您的服务器仍将只有在线状态才能显示最后一次状态.解决此问题的一种方法是使用 MQTT .>
在Activity
,onStart()
和onResume()
中,分别将在线状态消息作为1、0发送给代理.建立mqtt连接时,设置最后一条消息:
MqttConnectOptions options = new MqttConnectOptions();
options.setConnectionTimeout(60);
options.setWill(STATUS, ("PASS_OFFLINE_STATUS_OF_USER").getBytes(), 0, true);
client.setCallback(ApplozicMqttService.this);
client.connect(options);
来源: http://www.hivemq. com/blog/mqtt-essentials-part-9-last-will-and-testament
My chat app has the following approach:
- When user logs in on App, his status changes to Online. "Status = 1"
- When user logs out of App, his status changes to Offline "Last Available". "Status = 0"
The idea is that when logged user disconnects from internet, his status remains 1. Also if user removes the app his status remains 1.
Is there a code or way to detect when user was last online after removing/deleting app? Many users who removed the App still have their status as Online.
Inside your Activity , onStart()
and onResume()
, you can send the online presence message to the server as:
1 (means online)
0 (means offline) respectively,
But the problem is what will happen if the app comes to foreground or if internet stops or if user uninstall the app ?
Your server will still have the last presence as online only. One way to solve it is using MQTT.
In Activity
, onStart()
and onResume()
, send the online presence message to the broker as 1, 0 respectively. While making the mqtt connection, set the last will message:
MqttConnectOptions options = new MqttConnectOptions();
options.setConnectionTimeout(60);
options.setWill(STATUS, ("PASS_OFFLINE_STATUS_OF_USER").getBytes(), 0, true);
client.setCallback(ApplozicMqttService.this);
client.connect(options);
Last will message is used in MQTT to notify other clients about an ungracefully disconnected client.
Read more here:
http://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament
这篇关于如何知道应用程序用户是否离线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!