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.
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.
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've stumbled upon WakeLock. Is this the way to go or are these not suitable for my use case?
Since Lollipop, there's also the JobScheduler API which itself uses WakeLock. Any better?
How does, for example, WhatsApp handles this case?
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.
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 :)
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.
Related
I want to write a 'service' app that will be connected to a server, the server will send messages to the app and the phone will popup a notification to the screen instantly upon receiving the message from the server even if the phone is locked. (like Whatsapp/Telegram etc..)
Push Notifications are not instant enough.
I was thinking about using WebSocket for that, but not sure it is the best approach.
What is the best approach?
Sample code will be appriciated!
Thanks.
What I have studied on stackoverflow and Android documentation.
Finally I've concluded this:
There is no way to create a background service for continuous tasks. If I really want a service I should start a foreground service and user continuously sees a persistent notification "App is running". There is no way to hide this notification. It is intentionally added by Google.
Yes there are other options like WorkManager and JobScheduler but they do work periodically not continuously.
What I do want is to build an instant messaging app which continuously connects to the server using xmpp or sockets. But it requires a continuous connection but I don’t want to use a foreground service because it shows an irritating notification to the user "App is running".
Question 1: How does Whatsapp and other instant messaging app continuously connect to the server but not show a persistent notification ? How do they achieve this ?
Question 2: If Whatsapp use FCM for notifications then it will also work in those mobile which do not have playservices installed, so how does Whatsapp notification mechanism works ?
Starting with Android 6.0 (API level 23), Android introduces two power-saving features that extend battery life for users: DOZE and APP STANDBY. These two features enforce many restrictions on your background processing while the phone is in Doze mode. You should read about Doze and app standby in the following link
https://developer.android.com/training/monitoring-device-state/doze-standby
Now, about your use case is that you want to receive the messages and incoming calls even when the app is not running. For this use case, Android announced High Priority FCM messages in GoogleIO2016. They are high priority Push message which grant the application temporary wakelock and network access, independent of Device's Doze state or if the app happens to be in the app standby. This allows the application to react to the message and notify the user in whatever way it wants about the instant message or incoming call.
I don't know exactly how WhatsApp does that unless I look at their code but you can handle your use case using FCM High Priority Messages.
For more about your use case, follow the below link ofGoogleIO2016 Video from 08:30m to 10:30m
https://www.youtube.com/watch?v=VC2Hlb22mZM&t=505s
and read about this use case on the first link in this answer.
I'm working on an application that handles text messages. This is a personal application and I do not plan to release it, however it's basically going to allow me to share my text messages (and phone number) between numerous devices through the internet. It's a fun learning project too as a first application, and I've done quite a lot.
However the annoying part is the text-message popup that my device gets when receiving a message. I love it when I'm using the device, and I could always just go into the options and disable the popup when I'm not planning on using the device, but I'm a very, very forgetful person and turning it back on wont always happen. Then I'll never reply to messages.
Basically I want to programmatically interrupt (or not even notify) my default messaging application of the text message, however I still want it to be logged in my messaging history. So the message can't just be "discarded". This should only happen of-course while my applications service is running.
I've been searching through the android API for quite some time and I just can't seem to figure this out, is it possible and if so can you link me to the proper place in the API to begin?
Basically I want to programmatically interrupt (or not even notify) my default messaging application of the text message...
This isn't possible.
Assuming by "interrupt" you mean to prevent the default SMS app from issuing its Notification, you can no more do this with the default app than you could with any other app that you don't control.
Additionally, as the default SMS app responds to the SMS_DELIVER_ACTION broadcast, and it is the only app to receive this broadcast, it wouldn't be possible to "not...notify" the default app of an incoming message. Your app wouldn't even have a chance to abort the broadcast, even if it were possible to do so.
(In versions prior to KitKat, it was oftentimes possible to register a Receiver with a high priority for the SMS_RECEIVED_ACTION broadcast, and then abort the broadcast before the native SMS app received it. This is what rajan ks's answer refers to.)
...however I still want it to be logged in my messaging history.
The default SMS app is responsible for writing all incoming messages to the Provider. Even if you were able to prevent the default app from receiving the incoming message, your app would then have to write the message itself. This isn't really possible, either, as the default app is the only one with standard write access to the Provider.
Is there a way to notify the server when a device connects and disconnects from the GCM platform?
Connection can be implemented easily enough at the application level since I would be able to send a message to my server via CCS. Detecting when the device is offline does not appear to be so simple.
I was hoping that there was a control message I could tap into to detect this?
thenIn these types of scenarios's, the pinging methodology is the best. For example if your client suddenly looses all his batterypower and the phone suddenly dies. The client will therefore not be able to send a request to your server that he is no longer online. Simultaneously, on the server-side, he is still flagged as "online".
The pinging mechanism reduces this problem by proposing a solution such that "If a ping is not received in a timely manner then flag the user as offline".
The Openfire plugin at the server side is one of the best one available till now that does this job for you. You can give it a shot.
Hope that Helps!!
There is the roster flag which indicates this:
connectionConfiguration.setRosterLoadedAtLogin(true);
when building a server, one sometimes performs asynchronous tasks from client to server (which responds to client in asynchronous time),
or the server needs to send the client a message
now if the client is listening at all times (meaning polling) it takes a lot of resources which is problematic
here is where I assume the operating system steps in and assumes the role of polling for the appropriate port, and letting the application know using the appropriate event (the application subscribes using the OS API)
am I right in my assumptions?
how do I subscribe to a port using the OS's API? (lets say android for the sake of argument)
how is a message from server to client work exactly?
and how does the server know the client's IP at all times?
I have seen many questions in the subject, but wasn't able to figure out the big picture
Edit:
I am using GCM in android, but have seen other apps that does not use it and still manage to do it right, also it's a more general question as to what is the right approach in java VS. any operating system it uses (ubnutu, windows, android, etc.)
Totally right - polling is typically a waste of resources. Until recently, many apps would either keep a socket open and poll every few minutes to keep it alive, or make periodic HTTP calls to a server.
Nowadays, Google Cloud Messaging is used by most apps to push data instead of constantly polling. As you correctly guessed, this is implemented by maintaining a persistent connection with Google's servers. The advantage of this is that it's very efficient for battery life, and that all apps can use this one resource to send push notifications, instead of each app having to poll a different server or create its own persistent connection.
The idea is that you send requests to GCM from your server (this can be in response to user activity, etc), which sends it to all of the client's devices. You can either send a message with a small payload (up to 4kb) or a "send-to-sync" message, which tells an app to contact the server (e.g. to sync new data from the server after user changes).
here is where I assume the operating system steps in and assumes the role of polling for the appropriate port, and letting the application know using the appropriate event (the application subscribes using the OS API)
GCM pushes messages to clients, so there isn't active waiting like you'd see in a simple polling system.
how is a message from server to client work exactly? and how does the server know the client's IP at all times?
There's no need for servers to know the client IP, as any online android device will typically maintain a connection with GCM. Targeting specific users is done via User Notifications.
(Oh, and I realize that your question is more general than just Android, which I have more experience in, but iOS has a similar system in place. Some developers I've met like to use Parse for managing push notifications).