【问题标题】:Android Notification doesn't handle special charactersAndroid 通知不处理特殊字符
【发布时间】:2018-09-24 12:22:54
【问题描述】:

我正在使用 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();
        }
    }
}

当我收到通知时,它看起来像这样

为了在设备上正确显示通知,编码和解码通知的最佳方式是什么。

【问题讨论】:

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


    【解决方案1】:

    不要使用URLEncoder...,因为这是对输出进行编码的内容。

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

    只需确保.java 文件是UTF-8 编码的。

    【讨论】:

    • 我以前没用过,我收到了通知但没有特殊字母(类似“cole”而不是“école”)
    • @AnassBoukalane Android Studio 是否显示一些微小的UTF-8 指示器,右下角?这可能是错误编码的唯一来源;否则应该支持。
    • @AnassBoukalane 确保输入字符是UTF-8(这也可能来自资源文件)。底部的UTF-8 指示符是每个文件,而不是一般情况下。另见stackoverflow.com/a/40674362/549372
    • 我得到了它的求解器,谢谢大家的提示,问题出在我的发送客户端,它不在 UTF-8 上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多