春季启动服务中的UTF-8字符串



我正在为通过Firebase发送通知到我的Web应用程序的服务,这是我的代码:

  @Override
public String sendPushNotification() throws IOException {
    String result = "";
    URL url = new URL(Constants.FIREBASE_API_URL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "key=" + Constants.FIREBASE_SERVER_KEY);
    conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3)" +
            " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36");

    JSONObject json = new JSONObject();
    Notification notification = notificationRepository.getFirstByOrderByIdDesc();
    AssignTask assignTask = assignTaskService.getAssignTask(notification.getTaskId());
    try {
        json.put("to", accountService.findByEmployeeId(assignTask.getAssignerId()).getTokenFirebase().trim());

        JSONObject data = new JSONObject();
        data.put("sender", employeeService.getDetailEmployee(assignTask.getAssigneeId()).getFullName());
        data.put("task_id", notification.getTaskId());
        json.put("data", data);
        JSONObject info = new JSONObject();
        info.put("title", notification.getLabel());
        info.put("body", notification.getContent());
        System.out.println(notification.getContent());
        info.put("message", "");
        json.put("notification", info);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
    try {
        System.out.println(json.toString());
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(json.toString());
        wr.flush();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        String output;
        System.out.println("Output from Server .... n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        result = "succcess";
    } catch (Exception e) {
        e.printStackTrace();
        result = "failure";
    }
    System.out.println("FCM Notification is sent successfully");
    return result;
}

我调试时,这是JSON值

{"通知":{" title":"BáoCáoCôngViệc"," Body":" ^gửiBáoCáoVềC。

但是,消息发送给firebase的格式是错误的,我放了一个system.out.println(),它显示了:

{"通知":{" title":"BáoCáoCôngvi?c","身体":"??

我尝试了多种方式,但仍然没有帮助。有人知道为什么吗?我认为这是Spring Boot的UTF-8的问题。我正在使用Spring Boot和Gradle

这已经很晚了,但可以帮助某人解决此问题。

使用此命令运行Spring Boot Jar:

java -Dfile.encoding=UTF-8 -jar hello.jar

您可以从此设置获取有关-Dfile.encoding=UTF-8的更多信息。

相关内容

  • 没有找到相关文章

最新更新