user2761885
user2761885

Reputation: 337

Android - update client' sqlite database using GCM

I am working on an android app with SQLite database. I want to implement the following feature:

enter image description here

I read a lot of relate posts, many people recommended that GCM is the best approach to do such function by creating a GCM server to send notification to clients' device. For example, this tutorial.

However, it just display the message sent by GCM server on client's app. What should the GCM server send in order to update the client's sqlite database from server? (send a sql update command?)

Thanks in advanced!

============================================================

EDIT:

what I know at this stage: GCM server notify the clients there is a new update, then the client call a web service to fetch data and insert into its sqlite datbase.

Problems: what is the approach of writing such web service? how to sync the client app's database with my server one?

Upvotes: 0

Views: 1448

Answers (2)

Piyush Agarwal
Piyush Agarwal

Reputation: 25858

GCM server is not responsible to update you database, you can use it just to intimate your app that, yo! there is something new on my server to update, lets connect and download new data. It saves a lot of battery of device which you would be wasting in pooling your server on some defined time internal for checking the availability of update.

You can specify a key-value for json message which you are sending to your app from GCM like
{ "status" : "newupdate" }

and in onMessage() of GCMReceiver you can get the message by key "status" from intent and write logic like

if(message.equalsIgnoreCase("newupdate"){

 //Call a service and download the data and store/update in your databse 

}

Go for this tutorial if you are new for Sqlite Database http://www.vogella.com/articles/AndroidSQLite/article.html

To Update the database considering you already implemented he OpenHelper class

 MyOpenHelper myOpenHelper= new MyOpenHelper(this)
 sqltitedatabaseObject=myOpenHelper.getWritableDatabase();
 ContentValues values = new ContentValues();
 values.put(<your_column_name>,<value>);

// For Updating the Existing Entry
long rowAffected=sqltitedatabaseObject.updateWithOnConflict(<Table_Name>,
            values,<Selection_For_Update>,<Selection_Paramans_In_String_Array>,
            0);
// For Inserting a New Row
        long rowsInserted=sqltitedatabaseObject.insert(<Table_Name>, null, values);

As per your Edit let me add few points :

  1. Sync means keeping both data same, And to achieve this you have to create a webservice which will fetch the data from your database (add a time-stamp for each data inserted in database and send it along with the data you are sending to device ).

  2. While syncing data fetch the max value of time stamp from the device sqlite database and send as a parameter while hitting the web-service from device, and send data in response from server which are added or updated after that particular time-stamp received in request.

So even if the user deleted you app, you will get nothing in request time stampso send whole data.

Upvotes: 2

Abhishek Shukla
Abhishek Shukla

Reputation: 1242

send a sql update command?

No, you should send Json data, form your server, and in onMessage() of your GCMIntentReciever you'll get the same json data. Parse this json data into some object and execute insert query..

Upvotes: 0

Related Questions