如何知道应用程序用户是否离线? [英] How to know if app user is offline?

查看:265
本文介绍了如何知道应用程序用户是否离线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的聊天应用程序具有以下方法:

  • 用户登录App时,其状态更改为在线". 状态= 1"
  • 当用户退出App时,其状态将更改为最近可用"的脱机状态. 状态= 0"

想法是,当登录的用户断开与Internet的连接时,其状态仍为1.此外,如果用户删除了该应用,其状态仍为1.

在删除/删除应用程序后,是否有代码或方法可以检测用户上次上网的时间?许多删除了该应用程序的用户仍然具有在线"状态.

解决方案

在Activity,onStart()onResume()内,您可以通过以下方式将在线状态消息发送到服务器:

  • 1 (表示在线)

  • 分别为
  • 0 (表示离线)

但是问题是如果应用程序出现在前台或互联网停止运行,或者用户卸载了该应用程序,将会发生什么?

您的服务器仍将只有在线状态才能显示最后一次状态.解决此问题的一种方法是使用 MQTT .

ActivityonStart()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);

Source: https://github.com/AppLozic/Applozic-Android-SDK/blob/b7337f8d43c9e4df8927c77b34903c7762670cbf/mobicomkit/src/main/java/com/applozic/mobicomkit/api/ApplozicMqttService.java

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屋!

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