–Found the solution to my problem, code is updated–
I am having a problem sending German umlauts (öäü) via push-messages to the iPhone.
I am running Java/GWT on Google AppEngine and using UrbanAirship for the push notifications. The following code works perfectly on my mac, the push-notification arrives with the correct German umlauts in it. If I deploy it to the gae-server the German umlauts are not working. So far I found out that on GAE the standard encoding is US-ASCII and with some help here changed the getBytes() and everything else to UTF-8.
The problem is still there, but now the question marks which substituted the umlauts at the iPhone coming now with an “diamond” as background?!
Here comes the method I am using (works fine local, not at GAE):
private Boolean sendNotification(String appKey, String appMasterSecret, String jsonBodyString) {
try {
URL url = new URL("https://go.urbanairship.com/api/push/broadcast/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setConnectTimeout(12000);
String authString = appKey + ":" + appMasterSecret;
String authStringBase64 = Base64.encode(authString.getBytes("UTF-8"));
authStringBase64 = authStringBase64.trim();
connection.setRequestProperty("Content-type", "application/json; charset:utf-8");
connection.setRequestProperty("Authorization", "Basic " + authStringBase64);
OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
osw.write(new String(jsonBodyString.getBytes("UTF-8"),"UTF-8"));
osw.close();
int responseCode = connection.getResponseCode();
String responseMessage = connection.getResponseMessage();
if (responseCode == 200)
return true;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return false;
}
getBytes()without an encoding! You should be ashamed! Well, not really 🙂Use
getBytes(String)orgetBytes(Charset)instead — likely with UTF-8. (Similar issues affectnew String(byte[])and a few other methods.)Happy coding.