5

I want to embed data directly inside of a GCM (Google Cloud Message) in order to help my application skip a round trip to my server.

Google says the total data payload needs to fit inside 4kb. Data is sent using [key,value] pairs. Assuming I did something like this:

 String key = "key";
 String data = "data";
 Message message = new Message.Builder().addData(key, data).build();
 sender.send(message, regId, 5);

How can I know if the String data is less than 4kb? To my knowledge there's no way to really do a String.getSize() or similar.

Link to the documentation if curious: http://developer.android.com/training/cloudsync/gcm.html#embed

boltup_im_coding
  • 6,345
  • 6
  • 40
  • 52
  • Sorry, I meant the documentation of Google explaining embedding data directly into a GCM. – boltup_im_coding Oct 18 '12 at 17:52
  • I think you're looking for [this](http://developer.android.com/guide/google/gcm/server-javadoc/com/google/android/gcm/server/Message.html). Note you have to look at the static class [Message.Builder](http://developer.android.com/guide/google/gcm/server-javadoc/com/google/android/gcm/server/Message.Builder.html) for the addData() method. – boltup_im_coding Oct 18 '12 at 17:58
  • E.G. If using [Sender.post(..)](http://developer.android.com/guide/google/gcm/server-javadoc/com/google/android/gcm/server/Sender.html#post%28java.lang.String,%20java.lang.String%29) to send the message, note it throws an `IOException`. If it throws an `IOException` in the event of a too large message, the answer to the question might boil down to `try` it. – Andrew Thompson Oct 18 '12 at 18:00
  • I'm using `sender.send(...)`, not `sender.post(...)`. You are correct that I can try it. I was hoping to get a little more guidance on officially "how I know" so that in the event that it is too large, I gracefully handle it. – boltup_im_coding Oct 18 '12 at 18:06
  • I did not understand the 2nd sentence. It had short parts that made a kind of sense, but not when strung together. – Andrew Thompson Oct 18 '12 at 18:08
  • I was just saying you're correct I can "just try it" but I was wondering how I can programatically and officially know when I have exceeded that size so that I can gracefully handle the error, if it happens, in my application. – boltup_im_coding Oct 18 '12 at 18:13
  • *"I can gracefully handle the error, if it happens, in my application."* That is what `catch` is for! – Andrew Thompson Oct 18 '12 at 18:14
  • 1
    I realized just now that you meant `try` it and not "just try it" as in see if it works, lol. That's why our exchange made no sense. – boltup_im_coding Nov 14 '12 at 21:53
  • Glad you figured it out. Glad you got it sorted. :) – Andrew Thompson Nov 15 '12 at 00:46

3 Answers3

5

You can use the following to get the bytes of a String:

String s = "some text here";
byte[] b = s.getBytes("UTF-8");
int bytes = b.length;

Make sure that you specify the encoding of the String, as different encodings may take up a different number of bytes for the same String. In the above example, UTF-8 is used as the encoding.

To convert the bytes into kB, just divide by 1024.

EDIT: Here's a nice and detailed answer on the byte space of different encodings in Java from Geobits' comment.

Community
  • 1
  • 1
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
3
int byteSize = data.length() * 2  // each char is 2 byte

Updated

int byteSize = data.getBytes("UTF-8").length // if you want to know the size in your desired encoding
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
0

I run a couple of tests locally, seems like on the application side you can send a bundle that contains a key + value pair of about 4020 bytes before you start getting an error from gcm, while on the server side it's a bit more, about 4100 bytes. Given this, to play safe we have decided to go with 4,000 bytes on either side.

sakis kaliakoudas
  • 2,039
  • 4
  • 31
  • 51