Android notification when sql row is added - android

How would one go about creating a notification when a row is added to a remote SQL server?
For example, if user A inserts a row into the SQL table via a PHP script is there a way for user B to get some sort of response from the server to know to display a notification something has been added to the database?
Any help would be greatly appreciated

Depending on the underlying database (I've only done it in mySQL and Interbase/Firebird), there's usually a mechanism available to have an update/insert trigger either update another table (which you could pull) or broadcast a message on a socket (which could then generate a notification).
That said, a good web application is build around a framework, and those frameworks usually abstract the database interaction. In short, find the spot in the php script where the row is inserted, and hook your notification code in there. It'll be much more reliable, and more instantaneous.
As far as notifying the remote application, you might want to look into GCM.

Related

How to notify my android app when something change in MySQL Database

I have a question about real time notification in MySQL.
I want that when there is a change in a specific table of the MySQL db, the db notify my android app that do something.
Is it possibile to do? I have listen that trigger can help, but i didn't found anything about that in web, especially for android.
I'm creating a real-time chat app for an android project at the university. It is required to use a servlet in the project, so my servlet makes requests to the database and sends the data to the app. To do this the app makes a GET request to the servlet every second to get the new messages. I would like to avoid making these requests and updating the message list only when there is a change in the database. I know it can be done with firebase for example, but having to use the servlet I would like to know if there is a way to notify the app when this change occurs.
The messages are insert in the db with a POST request to the servlet that update db; information about messages are taken from an edit text in the app and with a send button i make the request, so the thing that i have to do is to push the android app when new messages are detected in the db.
Presumably you have some code component sending commands to the database. If yes, then that is where you could add code to post notifications to your app via websockets or something similar. Triggers in your database come at a cost and should be avoided if there is going to be a lot of them required.
As already mentioned, the app constantly polling, looking for a change is also expensive and probably best avoided.
'Is it possible?' questions are always difficult to answer. The answer is almost always 'yes', but 'is it advisable', 'is it a good idea', 'is it recommended', that is where it gets more complicated. I don't know of any way that a MySQL database change could notify individual Android app installations after a change. However, a running instance of an Android app could poll the MySQL database and look for the structure of the table, and if the structure of that table differed from the last time it polled, it could do something.
This would have a large overhead, in having the app continuously polling the MySQL database to check the structure of the table. That would add a lot of demands on your MySQL installation, eat up a lot of the app user's bandwidth and data plan.
So, 'is it possible', yes. 'is it recommended', no.
One question would be, what is the scenario where you think the table structure might change, and how often might that occur? What many people do, is check to see a database's table structures only once, when the app starts up a new session. If a change is detected on start-up, it could make the necessary local changes to its logic, then, assume the structure will remain constant through the rest of the session. This would eliminate the constant polling and other negative behaviors I mentioned before.

how to make a synchronous android application with sqlite

Unfortunately, I didn't find a better title for the question
i want to make application like a simple hotel reservation and i will make the database online if i add client to the hotel database via android application i want it pending and When I'm online i want the command execute but in the same time when i'm offline i want to see the that the command pending , it's like whatsapp when you send message while you are offline the message not send and you see it pending and when the internet available The message is sent automatically
so how i can do that ?
SQLite may well not be ideal solution. The reason being that SQLite is an embedded database not really designed for the client server model (the hotel database would, according to your decsription, be on a server). Appropriate Uses For SQLite
Perhaps Firebase Realtime Database
could be more appropriate.
However, if you wanted to use SQLite to just handle the requests from the device SQLite could perhaps be used for that aspect. For the requests to be sent automatically you'd need to look at perhaps running a service. Services overview. You would probably want to incorporate Notifications
If using SQLite then the first step, database wise would be to design the database, the table(s) and the columns within the tables. However, to do so requires that the system itself is designed. System design, would depend upon what can actually be done.

How to synchronize SQLite and MySQL databases using push notifications?

I have an SQLite database on Android and a MySQL database on a server. I want to synchronize these databases when a user edits data on their phone or edits data on a website.
I know how to update the MySQL database on the server when a user makes changes on their phone but I don't know how to update the Android database when a user makes changes on the website.
I have read into push notification and believe this to be a good path to follow but I have a few questions about it:
When a user updates data through a website it will send a push notification to that user's phone saying changes have been made. Can this push notification trigger to update the Android's database with the new changes made on the Server database?
What if a user turns off push notifications? Will I still be able to trigger for their Android database to be updated?
I have also read up on SQLite and MySQL database synchronization and found this post SQLite and MySQL sync but did not find the post helpful for my situation.
Are push notifications a good way to go or should I be using a different approach?
In a nutshell - I want a way for the Android device to detect changes on the MySQL database and update its SQLite database without the user initiating the synchronization.
I'm afraid I've not used push notifications. But a solution might be: You could create an early method call to an Asynchronous polling event from the launcher onCreate() that looks up the server to see if any changes have been registered (though an API of some sort) in the MySQL, and then update the SQLite that way? Since it's the first thing that happens on launch, technically the user isn't initiating it. Granted this won't update during use of the app, unless you repeat poll at regular intervals?
Token based pagination approach.
Assumptions: or calls you need to take
One of the databases will the source of truth, in case of differences in the two, which data is true, and which will be overwritten? - assuming remote database is source of truth
What's the frequency of data changes? - assuming its not realtime critical
How much stale data are we OK with dealing on the app. - assuming we're OK with a few minutes of difference
How to ensure data is consistent
Find a method to associate a token, which can be used to identify till which record is the data in sync. This is important no matter how assured you are of web requests, they will fail. So the best method is to send the last token that have stored, and the web endpoint will return data from that token to the latest value.
If the volume of data is too much here, sending chunks > sending all of it. Chunks work the same way, based on tokens.
These tokens can be simple PK auto increment column as well.
How to deal with time difference, stale data
If your application demands some data to be near realtime, better to categorize data based on a fiew screens, and whenever the user comes to the said screen, send a request in background to fetch related data columns only. Will ensure the data stays in sync w.r.t to the important columns. This is a classic push/pull approach. Can also be done on the splash screen itself.
as a rule of thumb, if you need something urgent, pull.
if it can wait, wait for a push.
Push notifications are OK as far as:
they should be silent.
there'a a limit on the number of push notifications that you can send
have costs associated
what's the fail - check mechanism? What if the requests fail?

How to observe the changes made in Remote sql database table from android

Currently am developing an android application in which i wish to intregrate the webservices and save the received datas in my sqlite database.Further if there is any changes in my remmote sql i wish to update the same thing in my sqlite too.How can i acheive this..is there any way to acheive this.I just stuck in this thing for more than 2 days.
You have many options to perform the task:
You can ping the server for updates, for example if you upon starting call some API method getItems and store the items on local database, you can after certain interval call getItems again and replace the old items with the new ones
You can use a service like GCM (Google Cloud Messaging) to archieve this so your server sends the updated items straight to your phone, but this really depends on the application you're using it on

How recive data from SQL Server to Android Application, in autonomous manner?

I have created a web service which, querying a SQL Server, retrives several tables.
I'm consuming this web service using KSoap2 package in Android application, and I want maintain alligned the tables on SQL Server and tables on Android Application.
Many of the tables in SQL Server remain the same over time, but some others change frequently. I need an automatic mechanism for update the tables on Android Application, on the initiative of Server.
I think I need a socket on Android application, for listening a signal transmitted from SQL Server, the meaning of which is "the data in some tables are changed". So, then, I can use the web service call for retrieves new data.
Is this possible? anyone has suggestions?
The answer for you question is a Android Service class. It can sit behind the program and listen to changes going on your SQL server and update it in SQLite database (or whatever you have)

Categories

Resources