我收到 google 的 FCM 通知,但我的消息正文是乱码。如何解决?
I got notification from FCM of google, but my messageBody was garbled. How to resolve it?
Post成功,但是邮件正文是乱码
{"data":{"data_content":"测试","data_title":"Joyce"},"notification":{"body":"测试","title":"test"},"to":"myDeviceId"}
this.is我的post代码,有错吗?谢谢
<pre><code>
public class HttpConnection {
public void startConnection(final String apiUrl, final String jsonStr, final OnPostNotificationListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection conn = null;
StringBuilder response = new StringBuilder();
try {
URL url = new URL(apiUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization","key=Key");
conn.setRequestProperty("Content-Type","application/json");
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
OutputStream os = conn.getOutputStream();
DataOutputStream writer = new DataOutputStream(os);
writer.writeBytes(jsonStr);
writer.flush();
writer.close();
os.close();
//Get Response
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
response.append('\r');
}
listener.onSuccessful(response.toString());
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
listener.onFail(ex.toString());
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
}).start();
}
public interface OnPostNotificationListener{
void onSuccessful(String result);
void onFail(String exception);
}
}
</code></pre>
感谢您的帮助。The picture of the phone
问题来自 writeBytes
方法和汉字(或其他非 ISO-8859-1 字符)的表示。您应该改用 writeUTF
方法:
writer.writeUTF(jsonStr);
你可以看看this post explaning the difference between writeBytes
and writeUTF
。基本上,在编写字符串时应该始终使用 writeUTF
。
Post成功,但是邮件正文是乱码
{"data":{"data_content":"测试","data_title":"Joyce"},"notification":{"body":"测试","title":"test"},"to":"myDeviceId"}
this.is我的post代码,有错吗?谢谢
<pre><code>
public class HttpConnection {
public void startConnection(final String apiUrl, final String jsonStr, final OnPostNotificationListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection conn = null;
StringBuilder response = new StringBuilder();
try {
URL url = new URL(apiUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization","key=Key");
conn.setRequestProperty("Content-Type","application/json");
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
OutputStream os = conn.getOutputStream();
DataOutputStream writer = new DataOutputStream(os);
writer.writeBytes(jsonStr);
writer.flush();
writer.close();
os.close();
//Get Response
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
response.append('\r');
}
listener.onSuccessful(response.toString());
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
listener.onFail(ex.toString());
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
}).start();
}
public interface OnPostNotificationListener{
void onSuccessful(String result);
void onFail(String exception);
}
}
</code></pre>
感谢您的帮助。The picture of the phone
问题来自 writeBytes
方法和汉字(或其他非 ISO-8859-1 字符)的表示。您应该改用 writeUTF
方法:
writer.writeUTF(jsonStr);
你可以看看this post explaning the difference between writeBytes
and writeUTF
。基本上,在编写字符串时应该始终使用 writeUTF
。