Android / Smack:在睡眠模式下保持XMPP连接处于活动状态 [英] Android/Smack: Keep XMPP connection alive in sleep mode

查看:668
本文介绍了Android / Smack:在睡眠模式下保持XMPP连接处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,它有一个聊天客户端作为其功能之一。聊天客户端使用基于适用于Android的 Smack 库并运行 Openfire 作为后台的XMPP服务器。使用 BOSH 建立连接。整个XMPP连接处理实现为运行和监听的服务即使不是应用程序的活动在前台,传入消息的背景也是如此。到目前为止,一切都运行得很好。

I have an Android application that has a chat client as one of its features. The chat client uses XMPP based on the Smack library for Android and running Openfire as XMPP server in the background. The connection is established using BOSH The whole XMPP connection handling is implemented as a service to run and listen in the background for incoming messages even if not activity of the app is in the foreground. So far, everything works perfectly fine.

唯一的问题似乎是睡眠模式。在模拟器中(设置为保持唤醒)或使用手机时,XMPP连接正在保持,应用程序可以发送和接收消息。但是,一旦手机进入睡眠模式,XMPP连接就会中断 - 我可以在Openfire服务器的管理控制台中看到用户离线。直观地说,我希望一直接收消息,例如WhatsApp。

The only problem seems to be the sleep mode. In the emulator (when set to "Stay Awake") or with the phone in use, the XMPP connections is holding and the app can send and receive messages. However, once the phone goes into sleep mode, the XMPP connection breaks down -- I can see it in the Admin Console of the Openfire server that the user is offline. Intuitively, I want to receive messages all the time like, e.g., WhatsApp.

当然,我在线搜索包括Stackoverflow,但我无法获得确定性回答。用例通常似乎是必须定期执行任务,例如每小时一次。但是,如果是聊天客户端,这似乎并不强烈。因为我认为这是一个常见的用例 - 毕竟,有很多聊天应用程序或带有聊天功能的应用程序 - 这些是我的问题:

Of course, I've searched online including Stackoverflow, but I couldn't get a definitive answer. Often the use case seems to be that a task has to be performed periodically, say, once every hour. But this doesn't seem to fir in case of a chat client. Since I assume this is a common use case -- after all, there a so many chat apps or apps with chat features out there -- these are my question:


  • 如何在电话休眠时更改/扩展我可以收到聊天消息的应用程序?

  • How to I have to change / extend the app that I can receive chat message while the phone is sleeping?

I我偶然发现了 WakeLock 。这是要走的路还是不适合我的用例?

I've stumbled upon WakeLock. Is this the way to go or are these not suitable for my use case?

自从Lollipop以来,还有 JobScheduler API本身使用 WakeLock 。还有更好的吗?

Since Lollipop, there's also the JobScheduler API which itself uses WakeLock. Any better?

例如,WhatsApp如何处理这种情况?

How does, for example, WhatsApp handles this case?

旁注:我使用模拟器进行调试时出现睡眠模式问题。当我在模拟器中关闭保持唤醒时,屏幕在1+分钟后变黑并且XMPP连接中断。但是我不知道如何在它变黑后重新唤醒/切换仿真器。 Android Studio实际上告诉我设备或其他东西已经消失,我必须重新启动模拟器。

On a side note: I have problems with the sleep mode using the emulator for debugging. When I switch off "Stay Awake" in the emulator, the screen goes black after 1+ min and the XMPP connection breaks. But I somehow have no idea how to wake up / switch the emulator back on once it went black. Android Studio actually tells me at some point that the device or something is gone, and I have to restart the emulator again.

推荐答案

解决此问题的确切方法是使用推送通知。

The exact way to resolve this issue is by using push notification.

在指定的空闲间隔后,即设备进入休眠状态时,XMPP连接的自然行为是断开的。

It is the natural behavior of XMPP connection to get disconnected after the specified idle interval i.e when the device goes to sleep.

来自WhatsApp的情况,它也使用相同的XMPP并维护一个服务器,它充当交换消息的包装类。此服务器检查消息状态是否已传递。 如果未发送,它会在收到消息时发送推送通知,现在在推送服务的设备端,它会检查连接是否处于活动状态并且是否经过身份验证。

Coming to the case of WhatsApp, it also uses the same XMPP and maintains a server which acts as a wrapper class on the messages exchanged. This server checks the message status whether it is delivered or not. If not delivered, it sends a push notification, now at the device end in the push service when a message is received, it checks if the connection is active and is authenticated or not.

如果未经过身份验证,则会重新建立连接。通过这种方式,大多数聊天应用程序都可以管理此超时异常。

If not authenticated, it re-establishes the connection. In this way, the most chat apps manage this timeout exception.

希望这有助于:)

这篇关于Android / Smack:在睡眠模式下保持XMPP连接处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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