Android Chat implementation Suggestions - android

I have been working on a chat application and I need some suggestions to go about it. I have a server. Whenever a user logs into my app, and selects some user by searching, and sends a message to him, it will be stored on the server. Then my app checks if any a new message has arrived for me. If any message comes, it will be notified to the user. For achieving this, I have followed this method to update the UI whenever a new message comes.
And when the chat continues for more than 3 minutes, the app starts becoming slow. How to go about the problem?

If the app is sluggish that means there is a memory leak somewhere, or you are over logging, or doing something that you shouldn't be doing on the UI thread. Maybe you're polling the server too much?
By the way using TextView for chat is primitive, use the transcript mode for ListViews.

Related

Realtime Database show if data is still hasn't been uploaded

so I'm working with realtime database and I'm trying to make a chat app for practice.
I want to add the message to my list then let the message item inside the list upload the message to the server, I'm also showing an indicator that tells the user if the message is being uploaded, there are other approaches to apply this but I want to go with this one, anyways.
there isn't any problem when there is an internet connection. the problem is when there isn't any internet connection, the message is added to the list and the indicator appears, when the internet connection returns everything works fine still.
but if I send a message (while offline) and then leave the chat room and return to the chat room, the messages will get loaded and the indicator won't appear altho it isn't uploaded to the server (the data is cached now).
I want to find a way to tell if the data has been uploaded or not? I don't want to check the server to see if the node exists, I can't do that to every message it will cost too much, thank you.
If you enable disk persistence, Firebase keeps all of its pending writes in its disk cache. When the app restarts, it reads those pending writes and starts trying them. This is usually the right behavior for your users.
Unfortunately there is no built-in way to persist completion handlers for the Realtime Database. So upon a restart it becomes impossible for you to detect when the pending writes have been committed on the server.
So this typically means that you need to do something custom to detect the situation, and will have to determine for yourself whether the use-case is worth the effort.
If your messages are in some way ordered/timestamped (for example, if you add them by calling push()) you can keep track of what the last message is for which you received a confirmation from the server. That way you will know when the client restarts, which messages may not have been sent to the server yet.
Your onDataChange or onChildAdded will be called for those unconfirmed message straight away though when your app restarts, so you'll need an additional mechanism to detect when those unconfirmed messages are written on the server.
The best approach I know if is to write a "dummy" message when the app starts. Since the pending writes are treated as a first-in-first-out queue, your new dummy message write will be sent to the server after all the pending writes from the previous run. So when your completion handler gets called for this dummy message, you can be sure that all messages before it have also been committed (or rejected in case they violate your security rules).
Firebase cloud functions fires an onFinalize event when a file has been uploaded to the storage. So you could probably write a cloud function like this.
exports.uploadedServer = functions.storage.object().onFinalize((object) => {
const filename = object.name
//mark this filename or filekey as upload complete
return
})
You should be able to find more explanation here.

Move processing to background in android

My use case: chat application. The user wants to send an image message to the other user. But the upload process takes a while to the firebase, the user may not stay in that fragment or app till the upload is complete. User may even close the app assuming his/her message will be sent.
Question: How do we guarantee the message delivery after the send button is clicked irrespective of the app is active or inactive.
You might want to take a look at the new WorkManager API. In their own words - "a library for managing deferrable (meaning it doesn't need to be done instantly) and guaranteed (guaranteed to happen eventually even if app is killed or restarted)"
You should probably still send the instant message using other means though, but the actual uploading of the image can be deferred to the WorkManager. See this video

Android Wear DataLayer API usage for synchronizing when phone app is closed

So, I want to learn this synchronization strategy instead of just using the simpler MessageAPI, but am really struggling with how to successfully implement this.
My project is like this: I make queries to download a small amount of text from an API, via my phone. I will make these queries every so often, haven't really decided on how often just yet. The data will update the watch, which should hold onto the last data received. After that first download occurs, I send data using a DataMap, to the Android Watch. I only send that once, because I believe that sets up a channel to continually send updates when ready. If that is wrong, please correct me.
My main question is this: what if the Android phone's app closes? Then the data object goes to null, and gets sent to the Watch as null? Or, should I send an object from a long-running service or shared preferences on the Android phone, so that the object is never null?
Think of the Data Layer as more of an event system, i.e., you update your data and you're notified on the other side when the data is updated (created, changed, or deleted). You don't have to worry about if the Activity is killed after that. Even if the data was 'deleted', you would be notified it was deleted.
On the Wear device, you would listen for the changes via a Service or Activity and update UI, DB, etc. accordingly.
It probably make sense to read through this Android training guide. (It isn't too long.) The Handling Data Layer Events section is probably the most useful.

Background Operation when the app is running

I was wondering if it is possible ( it should actually) to run a piece of code every couple of minutes even if the user is using the app. The basic Idea behind this is that the User will take the smart phone with my app loaded in the field and start collecting data, while he is collecting data or playing with other options of the app the phone catches the 3g/wifi zone it will start syncing the data to the server. I keep pushing notifications to the devices from my web service using Google Cloud messaging (GCM) and when it reaches the phone it means the phone is now connected to the internet I run that piece of code which do the sync for me. Would it stop the current activity of the app or I would say do the user will be able to keep collecting data and the sync is also running on the background?. any idea of this type of scenario?
thanks guys.
Anything that communicates with a server is by nature asynchronous in ActionScript (and you can't make it synchronous). So just do what you'd normally do to communicate with the server. Other processing, such as loops over a large collection or drawing a lot of objects, can make the application unresponsive.
Yes it is possible using AsyncTask. And your application will not stop while AsyncTask is running in background.

How to design an app which executes tests on webpage in background, and let the user use his phone normally?

I am currently trying to design a particular application and I don't really know how. The app has to run like this.
What I have to do :
The user launches the application. This action starts something independent of the UI (a Service currently), which has to request a remote server to get test scenario, execute the tests and send results to the same server, even if the UI of the application is not on the screen.
Second constraint : the user chooses, at the beginning, the time between two requests of scenario from the server.
Example : The user starts the app, chooses "do one request of test scenario every 5 min" and after, he can do what ever he wants on his phone. And the app, every 5 min, send a request to get test scenario (not one, but a various number, 50 for example), do the test scenario, and send the results to the server ; and then, wait for the next request.
During this, I think a notification icon in the status bar could be a good thing to access to the UI of the app. With that, when the user feels that the app has done enough tests, he can access to the UI and stop the app.
What I have already done :
I create an Activity which creates a Service. I also set an alarm with AlarmManager, received with an AlarmReceiver which calls the Service every X min/seconds/... and starts in the Service a request to the server to get the scenario (the http request is done in an AsyncTask). The service puts a notification in the status bar in its onCreate() method. I also managed to parse JSON, send HttpRequest and just technical things like that.
What my problems are :
I find that the Service stops itself at the end of the onStartCommand(). It finishes the method and then go to onDestroy() (where I don't put stopSelf()), but is the Service really closed ? If yes, how could I managed to have it "up" during all the duration of the application ?
The notification in the status bar just appeared and disappeared with the rythme given by the AlarmManager. How could I "stabilize" it ?
If you have already designed an app like that (a mail app, I think, is a good example, which request the mail server every X minutes to check new mail, and send a notification), how can I reach my goal ? To reuse the mail app example : is the service which checks the new mail on the server is always running ?
I really don't know how to design this king of app, so any help is welcome.
Sorry for my bad English. If you want precisions, code of the Service, the Activity, just ask.
Regards
Looks like your Service is running on the UI thread. You should create a new thread inside your Service, where all networking operations will work. So, there is no need in AlarmManager - just wait X milliseconds inside your Thread.
P.S. If you don't know how to create new thread:
new Thread(new Runnable(){
public void run(){
//your code here
}
}).start();
but I highly recommend you to read about multithreading first.

Categories

Resources