Quite a while now, I have been using GCM to sending push messages to my android app users. Now I want to be able to poll data from my remote database to the app users when remote database receives new insertion. I want to create a notification when there is a new insertion into the remote database.
I understand I would have to create background services which is set to every second check from the remote database, this has set backs as the battery life of the user would be highly affected.
Again, I learnt about alarm manager which also sets to a period of time to poll data from the database.
Now I want to understand the better way to poll data from remote database and creating a notification to the user even when the app is inactive or in the background.
There is, you can make the server send a notification to the devices when new data has been added. This can be done with server code or if you don't want to do all the data base code there are applications that you can use. If you have your own server you can use parse.com since they are taking their hosting services down January 2017. If you don't have you can use Firebase and Batch to get the job done.
Related
I am new in Android Development. I am fetching my database from the Xampp server and display data on listview it's working fine but when I update my database I need to refresh my activity every time. I want that I should not have to refresh it, again and again, it should automatically update the data in listview as a kind of real-time something! How can I achieve this? Please, someone, help me out... Thanks in advance!
Use notifyDataSetChanged() with the adapter instance that you have created for the List view.
This will notify any change in the data model fetched from the database.
Create a Service to listen to the port continously as per your need whenever there is a change in the data, you can send a signal to the application to fetch data again and then notifyDataSetChanged() will update the changes.
work of your refresh functionlity will be done by Service(not Activity).
Use push Notification concept
Android Push Notifications using Firebase Cloud Messaging FCM & PHP
when the server update send push notification from server and receive application by using services .
Inside activity make method to refresh listview call this method in the service.
I think Web socket and push notification both are a Right way to solve your problem. But as per my opinion, you should go with push notification it would be good because Socket is more costly to do it for that you need to make server strong. When traffic increased in the server then the number of socket connection would be increased and you need to manage that much of socket connections. FCM/GCM push notification is easy for that. You just need to send silent push whenever update data regarding particular user only.
For this best suit Firebase database if it is suitable for you in your current state.
I want to create a Android app which shows the list of users registered to my app and other functionalities.
Users of the application are fetched from server every time, when user open the activity or fragment. But it will show a lag of some seconds to user means user can't interact with the view. Is it ok?
On the other side, i think of storing the users in local database for the first time and load the users from the local database in next time. And If any new user will registered to my App, then i must send push notification to all users, which will store the details of the user in the local database.
I am confused on this. If anyone has better solution to this, please let me know.
Thanks.
I think you already know the right answer.
Implement a database in your app (SQLite) and use data from there on launch and only then update it (on background thread). Add a Service to your app and download new data in some interval using Job Scheduler (to save some battery life).
In Android, i have a socket keeping real-time communication with the server.
This app socket is being controlled by a service, that starts on boot and / or whenever a request is being emitted by the app.
Because I cannot depend on Google PlayStore, I fully control the sending + receving of push-messages manually.
Whenever a new push-message arrives from the server, the socket-service sends a local broadcast message and the listening activities can follow there own action.. If no activities are found, a default android notification is given to the user, saying '[ap] You have {n} new message(s)'...
This has its stability problems (the service can be shut down by the OS when low on memory for example) but its okay.
Now, consider the following:
I have multiple activities that listens-for and shows a count of unread messages.
HomePage
ConversationsOverviewPage
'The' conversation page (chatpage)
Each activity can be on the foreground, but can also be in memory for when the user back-presses and goes back 1 activity. So in Theory, there can be a situation where you want to update different/multiple activities at once.. This prevents having to 're-load' the unread messages from the server when the user gets back to a 'savedInstance'. So the broadcast pattern works best I think.
What is best practise for keeping global track of unread messages, while minimising the server trip on every activity instance:
Very simpel: Make a server request on each and every activity instance, and write update code again for every activity. But this results in the user seeing a delay, as it takes a second before the app receives data from the server and show the 'unread-messages' balloon.
Simpel: Have a global class.. Holding the unread messages for each conversation, But I feel this can give problems with data being incomplete.. Especially when the app is not 'active'
My old vote: Have another service thats keeping track of the unread messages, that starts on boot (just like the socket).. Only when the service starts / boots, it will requests all unread-messages data from the server. Each activity can than 'ask' for the unread-messages data and don't have to worry about it anymore.
But this could be overkill?
My new vote: Keep the socket-service, and add a separate class to this service.. That holds the unread-data.. But this also does not feel to be right.. As the activity would have to ask the service something out-of-scope.. Its not the sockets concern to manage unread messages (separation of concerns), right?
Any thanks from experienced developers is much appreciated!
Third options is ok. Not sure where is overkill exactly. Obviously you shouldn't download all unread messages on every boot or socket reconnection. The most important rule of thumb is to load data when the app really needs it. Few moments about how I've developed about the same app:
there is socket Service which handles connecting, disconnecting, sending data(messages) and receiving data.
there is Notification manager which receives events from SocketService. It saves new data which comes from server and decides which broadcast notification should be sent.
when socket is connected it receives state data from server:
dates(timestamp) of last update for every chat. For instance if local database contains that chat A was updated yesterday but freshly received data from server says that the chat A was updated few seconds ago you need to broadcast event like chat A has been updated since last connection and save update date. If there there is any activity which somehow show the chat A it loads(through http or whatever) new data.
last messages for every chat. The app just compares locally saved last messages and freshly received. If there is new one the app again broadcast event like there is unread message/messages from user x. If there is visible activity which shows updated chat it updates data otherwise the app shows notification.
So the basic flow of handling unread messages is next: connect to the server > check if there is data about unread messages > save new data to the local database > broadcast events about new data.
And I would recommend you to use GCM and socket connection simultaneously. GCM really helps to keep data updated. It wakes up a phone and sometimes delivers data when socket connection just could not be established due to network problems.
Detail:
I have developed an android native application on ADT.
App performs ADD , Search and update operations on SQL database hosted on Windows Azure cloud platform.
All the operations are performed by calling a Mobile Service built on Windows Azure Cloud platform.
Problem Description:
Every time I want to ADD a new record via UI of app, my app calls mobile service and get hang till the operation completes.
I want to do this operation but do not want user to wait for it to complete.
Additionally when there is no network, user addition should be kept in queue for later addition.
Ideal Scenario: I want to display Addition operation in pending status to user and make that as a background process till the time it completes.
Note: There can be many ADD operation going on by multiple users. Need to keep that in mind.
Please suggest options to do this.
Free to ask questions in case of any query regarding problem statement.
Thanks
Anshul
You have to use AsyncTask to do work in the background and update the UI for the User or Pop a notification in OnPostExecute which means background work has been done.
For Queuing when No Network:
You would have to user a Service with BroadCastReceiver which listener for Network-Connectivity. Once the BroadCast Receiver Receives that Internet is Connected, You can then start calling the mobile service.
But Let me add that the Queued Data has to be Saved so you would have to create a SQlLite database or save the Data to a file on the Phone, which you can retrieve the data from, when the BroadCastReceiver is notified that connection is back
This is for an app for a clinic giving course in stop smoking and nicotine addiction,
We want to make sample app for android, where we scheduled the treatments course time plan, that’ remind the patients when to take the medicine.
So when the patient start their treatment, they just register at the day of starting the treatment, then automatically the app will scheduled all the following treatment, and send a reminder when next treatment arrive.
I know this topic is broad to answer but I just want to know how to make the database (schedules of the prescription) set for the timer to activate?? Can I use an external database that is network based..If so how to do that???Is sqlite a better option??? And how to automatically get those data and schedule it in the android app??
Since the data will be shown to all users like the time table so they can reserve if there is an empty date, then you require to have a cloud database and i suggest you to start learning how to connect android with MySQL here is a great video tutorial of how to connect android with MySQL by the help of PHP and JSON.
Now if you want this application to work offline, you require to have a SQLite database that when you connect to the internet once, you save these data in it. and whenever the device connects to the internet it updates the database entries.
for alerting the paitents you will have a service that runs to check the data , then start a notification. It may be possible to have an event listener for some given event that would start your service that would then check the date, and if so post your notification!
here is a good tutoial about it.
I hope I have covered most of your questions. There may be a better solutions around so dig more. Good luck with your app.