Android Client-Server architecture: GCM versus web

2019-08-13 12:34发布

问题:

I have an Android application with a local database that contains information about pictures stored on the device. I want to create a server with pictures that could be provided on demand to all devices that have my application installed. So my application can provide local pictures or pictures from my server and also needs some information related to those pictures.

I analyzed GCM and it great for message communication, but since the size of the message is limited to 4kb I can't send the pictures. My idea was to have something like this:

My question is: in this context is the GCM communication useful or it would be more efficient to have only client-server communication through a webservice?

I assume that if I use GCM, in the server-client communication I only use the code bellow as in "Bitmap Fun" example to receive the picture

 BitmapFactory.decodeFile(pictureURL, options);

The approach above requires more battery and bandwidth since I am asking for 1 picture at a time instead of an array of 50pictures? If I don't use the GCM, what should I handle (e.g. device is in stand by)?

回答1:

GCM is only useful if the server needs to alert the client application about some new data (in your case new pictures). It allows the server to send data to the client even while the client app is not running (or running in the background). In that case the green line makes sense - the server can send picture info to the client via GCM. Then, as you specified with the red line, the client would ask the server for the picture itself.

The blue line that you marked with ask for pictures should go directly from the client to the server. The client can't send requests to the GCM server (except of requests to register/unregister to/from the GCM service).

If the sending of pictures from the server to the client is always initiated by the client, you don't need GCM.

In addition, you can check this question to learn more about sending images via GCM.