How To Send Data From Server To Android?
Solution 1:
I really don't know where you're getting your information from, but both you and MD
are wrong and GCM is the best option.
from your question:
I planned to use Push Notification (GCM) for this, but push notifications will not be a good option as I didn't want the user to know that we are adding data in mobile app.
GCM is related to showing notifications to the user, but it's not what it does.
GCM is "Google Cloud Messaging". It only sends a message to your app. This message is received inside a BroadcastReceiver
. From inside this BroadcastReceiver
you can execute any actions you need, like syncing the information with your server.
I'll show a possible example implementation of the BroadcastReceiver
for the GCM.
That's a simplified example and NOT a complete implementation:
publicclassGcmBroadcastReceiverextendsWakefulBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
Bundleb= intent.getExtras(); // you send those extras from your serverinttype= b.getInt("type");
switch(type){
case TYPE_SYNC:
// start a `Service` to sync data from your server break;
case TYPE_ADD_DATA:
longid= b.getLong("id");
Stringname= b.getString("name");
Stringdescr= b.getString("descr");
// call code to add id, name, descr to your local databreak;
case TYPE_NOTIFICATION:
Stringtitle= b.getString("title");
Stringmessage= b.getString("message");
// call code to make a notification with title and messagebreak;
}
}
}
on this example your server can send 3 different types of GCM.
- TYPE_SYNC: will make your app start a background service that will connect to the server and sync information
- TYPE_ADD_DATA: will send the data directly inside the message and that gets directly added to the device storage (SQLite probably)
- TYPE_NOTIFICATION: this is the only option that the user gets notified about anything. The other two options are transparent for the user.
for a complete implementation and how to properly use the WakefulBroadcastReceiver
please check the official docs: http://developer.android.com/google/gcm/client.html
Post a Comment for "How To Send Data From Server To Android?"