Android / Smack: support XMPP connection in sleep mode

I have an Android app in which the chat client is one of its features. The chat client uses Smack- based XMPP for Android and runs Openfire as an XMPP server in the background. The connection is established using BOSH. All XMPP connection processing is implemented as a service for starting and listening in the background for incoming messages, even if there is no application activity in the foreground. So far, everything is working fine.

The only problem is sleep mode. In the emulator (when set to "Stay Awake") or using a phone, XMPP connections are held and the application can send and receive messages. However, as soon as the phone goes into sleep mode, the XMPP connection breaks - I see it in the Openfire server admin console that the user is offline. Intuitively, I want to receive messages all the time, for example, WhatsApp.

Of course, I searched on the Internet, including Stackoverflow, but I could not get the final answer. Often a precedent is that the task should be performed periodically, say, once an hour. But this is not like fir in the case of a chat client. Since I assume this is a common use case - after all, there are so many chat apps or apps with chat features, this is my question:

  • How can I change / expand the application so that I can receive a chat message during sleep?

  • I came across WakeLock . Is this a way to go or are they not suitable for my use?

  • With Lollipop, there is also the JobScheduler API, which itself uses WakeLock . It is better?

  • How, for example, WhatsApp handles this case?

On the one hand: I have problems with the standby mode, using the emulator for debugging. When I turn off "Stay Awake" in the emulator, the screen turns black after 1+ minutes and the XMPP connection is disconnected. But somehow I have no idea how to wake up / switch the emulator back as soon as it gets dark. Android Studio actually tells me at some point that the device or something is gone, and I have to restart the emulator again.

+5
source share
2 answers

The exact way to solve this problem is to use push notification.

The natural behavior of the XMPP connection is disabled after the specified timeout period when the device goes into sleep mode.

Getting started with WhatsApp, it also uses the same XMPP and supports a server that acts as a wrapper class for messaging. This server checks the status of the message whether it is delivered or not. If it is not delivered, it sends a push notification, now on the end of the device in the push service, when a message is received, it checks whether the connection is active and authenticated or not.

If it is not authenticated, it restores the connection. Thus, most chat applications manage this timeout exception.

Hope this helps :)

+2
source

You do not need push notifications, you do not need WakeLock s. Instead, just

  • Whitelist your doze application
  • Use a sticky ( START_STICKY ) background service
  • Use Smack ServerPingWithAlarmManager
  • The CONNECTIVY_CHANGED Act sends Android and uses XMPPTCPConnection instantShutdown() in this case.
+2
source

Source: https://habr.com/ru/post/1263228/


All Articles