我正在尝试使用 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 - 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;
}
}
关于android - FCM : Displaying emoji or UTF-8 text on iOS that is sent from Android device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48239347/