How to send data from server to Android?

左心房为你撑大大i 提交于 2020-01-12 05:51:13

问题


I am working on a project where I want my server to send some data to my application (without calling of web service from mobile). It is like a web panel which operates mobile app to add data. So when user add data in website and click add, it should add that data to the mobile app (if mobile is connected to the internet). It is also like sending command to android app. I planned to use Push Notification (GCM) for this, but push notifications will not be a good option as I dnt want user to know that we are adding data in mobile app. It should add even if the app is not in active state or opened.

I think I have below 3 options

  1. Make session between server and mobile. Start client server communication

  2. Use SMS to send command then call web service as per the requirement

  3. call web service after every 15-20 seconds to check for any update. (Even in Background)

Please advice if I have any other option to achieve this.


回答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:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getExtras(); // you send those extras from your server
        int type = b.getInt("type");
        switch(type){
            case TYPE_SYNC:
                 // start a `Service` to sync data from your server 
            break;
            case TYPE_ADD_DATA:
                 long id = b.getLong("id");
                 String name = b.getString("name");
                 String descr = b.getString("descr");
                 // call code to add id, name, descr to your local data
            break;
            case TYPE_NOTIFICATION:
                 String title = b.getString("title");
                 String message = b.getString("message");
                 // call code to make a notification with title and message
            break;
        }
    }
}

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



来源:https://stackoverflow.com/questions/29340596/how-to-send-data-from-server-to-android

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