1

I am building an app which needs to support Hebrew letters (שלום). I am sending a message thru GCM (Google Cloud Messaging). Whenever my OnMessage is called the string (Hebrew) that I am receiving is empty.

Is there any easy workaround for this? Thanks in advance!

protected void onMessage(Context context, Intent intent) {
  String message = intent.getStringExtra(GCM_COMMAND);
  String action = intent.getAction();
  if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
    message = intent.getStringExtra(GCM_COMMAND);
    String s = message.toString();
    ItemBean ib =parseJson(s,context);
  }
  private ItemBean parseJson(String text,Context con) {
    ItemBean item = null;
    if (text != null) {
      JSONObject temp;
      try {
        temp = new JSONObject(text);
        String itemMessage = temp.get(GCM_MESSAGE).toString();
        //This String is Empty!
        String itemDate = temp.get(GCM_DATE).toString();
        long currentTime = System.currentTimeMillis();
        item = new ItemBean(currentTime,itemMessage,itemDate);
        sendNotification(con,itemMessage,itemDate);
      }
    }
    return item;
  }

Server code from NetBeans

JSONObject json = new JSONObject();
System.out.println("******גגגגגגשלום*********");
json.put(GCM_MESSAGE, ib.getmMessage());
//This is hebrew no problem!
json.put(GCM_DATE, ib.getmDate());
Message.Builder builder = new Message.Builder();
Message message = builder.build();
MulticastResult result = sender.send(message, listOfRegisteredDevices, 5);
int success = result.getSuccess();
dda
  • 6,030
  • 2
  • 25
  • 34
user1163234
  • 2,407
  • 6
  • 35
  • 63
  • What does the JSON message look like before you parse it? – Ted Hopp Nov 18 '12 at 08:46
  • Its empty there as well...Getting the date but message is empty. you think its a problem with server code? – user1163234 Nov 18 '12 at 08:47
  • The code shows how you receive a GCM message. How are you sending it? – Ted Hopp Nov 18 '12 at 08:49
  • added server code above. Thanks! – user1163234 Nov 18 '12 at 08:58
  • I'm thinking there might be an encoding problem somewhere. (The message needs to be built using UTF-8.) If you print `json.toString()` on the server after putting the message, does it show Hebrew properly? – Ted Hopp Nov 18 '12 at 09:24
  • the json.toString is fine its shows the hebrew properly – user1163234 Nov 18 '12 at 18:21
  • Hm. Maybe it isn't an encoding issue, since the message is empty rather than garbage. Is Hebrew even a factor? What happens if you send an English message? – Ted Hopp Nov 18 '12 at 18:41
  • English is fine...{"date":"18\/11\/2012 - 19:48:31","message":"hello"} – user1163234 Nov 18 '12 at 19:04
  • look at this post it seems like the same issue I am having but I have no ide how to implement..http://stackoverflow.com/questions/7407566/hebrew-strings-submitted-to-web-server-arent-received-in-hebrew...Also I dont know if this helps but the values are being scraped from the web... – user1163234 Nov 18 '12 at 19:22
  • when I copy the text from the web the characters are in question marks ????????....Any idea? – user1163234 Nov 18 '12 at 19:49
  • If the text is question marks, that's definitely an encoding issue. Somewhere a conversion is being made that is not UTF-8. – Ted Hopp Nov 18 '12 at 19:50
  • How can I convert the string back to utf-8? – user1163234 Nov 18 '12 at 19:51
  • You can't convert the string back to utf-8. You have to avoid it becoming corrupted in the first place. I'm still unclear on where in the data flow the Hebrew is becoming corrupted. Is it on the server, in transmission through the GCM servers, or somewhere after the message is received? – Ted Hopp Nov 18 '12 at 20:14
  • I think this is it http://stackoverflow.com/questions/11501504/android-gcm-unicode-charcters-are-not-received – user1163234 Nov 18 '12 at 20:16
  • That looks like it should address your problem. You'll just have to URL-decode it on the receiving end. – Ted Hopp Nov 18 '12 at 20:33
  • Thanks for all your help!can you point me to an example on how to URL-decode? – user1163234 Nov 18 '12 at 20:35
  • 1
    Take a look at the [URLDecoder](http://docs.oracle.com/javase/6/docs/api/java/net/URLDecoder.html) class – Ted Hopp Nov 18 '12 at 21:18

1 Answers1

0

Can you try :

            String itemMessage = temp.getString(GCM_MESSAGE); 

and also

            json.put(GCM_MESSAGE, JSonObject.quote( ib.getmMessage() ) );
Snicolas
  • 37,840
  • 15
  • 114
  • 173