FCM:在Android设备上发送的iOS上显示表情符号或UTF-8文本
问题描述:
我正在尝试使用Firebase发送推送通知(表情符号或UTF-8文本). 在iOS和iOS之间,在Android和Android之间以及从iOS到Android之间,它运行良好.但是仅从Android到iOS,它不起作用并且文本已损坏.
I'm trying to send a push notification(emoji or UTF-8 text ) using Firebase. Between iOS and iOS, between Android and Android and from iOS to Android, it works well. But only from Android to iOS, it doesn't work and the texts are broken.
我使用URLEncoder.encode函数进行UTF-8编码. 这是发送代码.
I used URLEncoder.encode function for UTF-8 encoding. Here is the code for sending.
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();
请帮助我.提前致谢.
Please help me. Thanks in advance.
答
我终于找到了解决方案. Android-如何在Android中将字符串转换为utf-8
I finally found the solution out. Android - How to Convert String to utf-8 in android
感谢@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;
}
}