Upstream files using the Google Cloud Messaging Cloud Message Server (XMPP)

I use real time chat using the GCM Cloud connection server. I read docs upstream posts using GCM . I learned how to raise a message to my server through GCM. This is more reliable, as we all know, and better than sending messages to a web server via http.

But now I also want to upload files to chat.but, the problem is that GCM allows an upstream maximum of 4KB messages. I can use web services or FTP to upload files to the server, and then transfer the file name to the server via GCM. but I want a more suitable solution. So is there a way to do this through the GCM cloud connection server.

Thanks for the help in advance.

+6
source share
2 answers

The solution I use in these cases is:

1 - Use loopj library to send a file via POST to my server (php p.ex)

// loopj supports Http Auth, cookies, params, multipart files, etc. So is a good solution. Like: ... AsyncHttpClient client = new AsyncHttpClient(); client.addHeader("Authorization", "Basic " + Base64.encodeToString("aaa:bbb".getBytes(), Base64.NO_WRAP)); RequestParams params = new RequestParams(); params.put("cmd", "upp"); params.put("uid", Long.toString(getUser().getId())); params.put("tid", Long.toString(getUser().getIdTeam())); try{ params.put("avatar", file); // File object }catch(FileNotFoundException e){ ... } SharePhotoHandler handler=new SharePhotoHandler(mContext,file,notificate); client.post(URI_BASE, params, handler); ... 

And I use a custom handler to control file upload (SharePhotoHandler.class), where the application can control

  • Onsuccess
  • Onfailure
  • onfinish
  • ...

These methods should allow you to monitor the status of the fileโ€™s upload / download (ok, error, upload, download, etc.).

After downloading the file, the server should notify other clients that there is a new file to download, or, using your onSuccess method, send a GCM message to other clients. In my case, the server sends the file name (+ URL) to other clients to download it.

0
source

you asked:

So, is there a way to do this through the GCM cloud connection server.

And the answer for files larger than ~ 4k is no!

Explanation: The GCM engine consists of many servers and bandwidth, which are expensive for Google. Despite this, Google provided this service worldwide for free to encourage developers and users to use Android, allowing them to get a more effective experience.

Google knew that installing 10+ applications, while each of them had to open a connection to another server in order to receive its updates, would slow down the device and consume expensive bandwidth. Thus, they implemented the GCM solution, and thus each device has only one service (Google), which is constantly looking for updates (instead of many).

Thanks to the free service, Google minimized the service to 4 thousand data. This is good enough, because the application will receive information about what the update is waiting for and can then connect to server servers to receive these updates (instead of constantly searching for them).

0
source

Source: https://habr.com/ru/post/970158/


All Articles