Android/Smack: Keep XMPP connection alive in sleep mode

怎甘沉沦 提交于 2019-12-03 14:41:52
ChaitanyaAtkuri

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

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

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.

Hope this helps :)

Flow

You don't need push notifications, you don't need WakeLocks. Instead simply

  • Whitelist your app from doze mode
  • Use a sticky (START_STICKY) background service
  • Use Smack's ServerPingWithAlarmManager
  • Act on CONNECTIVY_CHANGED intents send by Android, and use XMPPTCPConnection's instantShutdown() in that case.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!