Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM send image instead of message

I'm an Android newbie, and I'm using Google GCM to send a plain text message. Is it possible to send an image file as stream or by some other method?

Java - Server

Android - Client.

    Message message = new Message.Builder()
                .collapseKey("1")
                .timeToLive(3)
                .delayWhileIdle(true)
                .addData("TEST",
                        "Hello Android")
                .build();

Here the plain message is .addData("TEST","Hello Android").

like image 557
Ashraf Avatar asked Mar 15 '13 15:03

Ashraf


3 Answers

You can only send key/value pairs with total size up to 4096 bytes. Even if you manage to encode an image within a string parameter, it would be a tiny image. An alternative is to send a string that refers to the image location, either a local file name on your device or a URL that you can access to download the image when you handle the notification.

like image 187
Eran Avatar answered Oct 30 '22 14:10

Eran


I wrote two blogs posts on how to do this:

Tutorial: Using AirBop to Send Images in the Message Payload which shows you how to do it by base64 encoding the image.

Tutorial: Using AirBop to Push Images for BigPictureStyle Notifications which shows you how to push image urls and then download the image.

Both tutorials use AirBop as the Application server, but the client code is separate from that and can be used generically.

like image 23
selsine Avatar answered Oct 30 '22 12:10

selsine


You could only send a very small image, as the data payload is limited to 4kB. You would also need to encode it somehow.

like image 28
NickT Avatar answered Oct 30 '22 14:10

NickT