Android - update client' sqlite database using GCM

一笑奈何 提交于 2019-12-11 23:23:19

问题


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

  • when there is new data available, clients' app will download the new data to its sqlite database automatically.

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?


回答1:


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.




回答2:


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..



来源:https://stackoverflow.com/questions/20580240/android-update-client-sqlite-database-using-gcm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!