GCM send image instead of message

2020-02-06 08:08发布

问题:

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").

回答1:

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.



回答2:

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.



回答3:

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



回答4:

Okay let's think about the bigger picture here. As everyone has said it is very hard or impossible to send an image using gcm unless you have a tiny image. Another technique is to send it in parts but hey that's annoying no one wants to code that all out. My suggestion is to code the server to store passkeys and image credentials which can be sent to the device. The device then queries the server with the credentials to get an image download. This technique is similar to the theory of using push to notify the device that new data is available on the server. It was originally used to make a server that uses as little push as possible but it is adpted in this case to be a placeholder for sending big data.