Android Notification不处理特殊字符

时间:2018-09-24 12:22:55

标签: android firebase encoding push-notification firebase-cloud-messaging

我正在使用Firebase消息服务发送通知,但是在设备和仿真器上显示特殊的加重字符时出现问题。

这是我发送数据的方式:

public static String sendPushNotification() throws IOException {
    String result = "";
    URL url = new URL(API_URL_FCM);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "key=" + AUTH_KEY_FCM);
    conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

    JSONObject json = new JSONObject();


    json.put("to", "fT3MxvEyzs8:APA91bH-J3Tj.....");

    JSONObject info = new JSONObject();
    info.put("title", URLEncoder.encode("éééé","UTF-8")); // Notification title
    info.put("body", URLEncoder.encode("ééé","UTF-8"));
    json.put("notification", info);


    JSONObject data = new JSONObject();
    data.put("title", URLEncoder.encode("ééé","UTF-8")); // Notification title
    data.put("body", URLEncoder.encode("ééé","UTF-8"));
    json.put("data", data);

    try {
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(json.toString());
        wr.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output;
        System.out.println("Sending Notification .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        result = "OKKKKK";
    } catch (Exception e) {
        e.printStackTrace();
        result = "NNNOOOOKKKKK";
    }
    System.out.println("FCM Notification is sent successfully");

    return result;
}

这就是我在应用程序中处理通知的方式:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.i("fcm", "received notification");
    Log.i("fcm", "Message received");
    // Not getting messages here? See why this may be: https://firebase.google.com/support/faq/#fcm-android-background
    Log.i("fcm", "From: " + remoteMessage.getFrom());
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.i("fcm", "Message data payload: " + remoteMessage.getData());
    }
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.i("fcm", "Message notification: " + remoteMessage.getNotification());
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (remoteMessage.getNotification() != null && remoteMessage.getNotification().getTitle() != null && remoteMessage.getNotification().getBody() != null) {
            try {
                sendNotification(URLDecoder.decode(remoteMessage.getNotification().getTitle(),"UTF-8"), URLDecoder.decode(remoteMessage.getNotification().getBody(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    } else {
        try {
            sendNotification(URLDecoder.decode(remoteMessage.getData().get("title"),"UTF-8"), URLDecoder.decode(remoteMessage.getData().get("body"),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

当我收到通知时,它看起来像这样 enter image description here

编码和解码通知以便正确显示在设备上的最佳方法是什么。

1 个答案:

答案 0 :(得分:1)

请勿使用URLEncoder ...,因为这是对输出进行编码的方式。

仅在直接使用API​​时才需要设置UTF-8-

只需确保.java文件已被UTF-8编码。