我正在尝试使用 Firebase 发送推送通知(表情符号或 UTF-8 文本)。在iOS和iOS之间,在Android和Android之间,从iOS到Android,它运行良好。但是只有从Android到iOS,它不起作用并且文本被破坏了。
我使用 URLEncoder.encode 函数进行 UTF-8 编码。这是发送的代码。
JSONObject jsonObject = new JSONObject();
jsonObject.put("to", mCurrentUser.token);
JSONObject notificationObject = new JSONObject();
notificationObject.put("title", title);
notificationObject.put("body", URLEncoder.encode(mMessage.text, "UTF-8"));
notificationObject.put("content_available", true);
jsonObject.put("notification", notificationObject);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
请帮我。提前致谢。
我终于找到了解决方案。Android - 如何在 android 中将字符串转换为 utf-8
感谢@Paras。
public class StringFormatter {
// convert UTF-8 to internal Java String format
public static String convertUTF8ToString(String s) {
String out = null;
try {
out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
return null;
}
return out;
}
// convert internal Java String format to UTF-8
public static String convertStringToUTF8(String s) {
String out = null;
try {
out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
} catch (java.io.UnsupportedEncodingException e) {
return null;
}
return out;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句