Firebase错误400发送HttpPost消息

时间:2018-01-11 16:54:54

标签: java android firebase http-post firebase-cloud-messaging

我正在尝试将Messaje从Java服务器发送到特定的Android设备,但它返回了一个错误:400 Bad Request

该应用程序的关键是可以的,因为我一直在尝试使用其他错误的密钥,直到在Firebase控制台中建立

代码是:

    public static void sendMensaje(){

        LOGGER.log(Level.INFO, "zzz Empieza");
        String respuestaString = new String("OK \n");
        try {

            HttpClient httpClient = new DefaultHttpClient();

            HttpPost post = new HttpPost("https://fcm.googleapis.com/fcm/send");
            post.setHeader("Content-Type", "application/json");
            post.setHeader("Authorization", "key="+" AAA...K0ynKNJN");

            String mensajeJSONEnString = buildNotificationMessage().toString();
            StringEntity stringEntity = new StringEntity(mensajeJSONEnString,"UTF-8");
            post.setEntity(stringEntity);

            HttpResponse response = httpClient.execute(post);
            int responseCode = response.getStatusLine().getStatusCode();

            if(responseCode == 200 ) {
                respuestaString = "OK!";
            } else {
                String respuestaString2 = new String();
                if (responseCode == 500) {
                    respuestaString2 ="Error interno del servidor, prueba más tarde.\n";
                } else if (responseCode == 503) {
                    respuestaString2 ="Servidor no disponible temporalmente, prueba más tarde.\n";
                } else if (responseCode == 401) {
                    respuestaString2 ="La API Key utilizada no es válida.\n";
                }
                respuestaString = new StringBuilder(respuestaString2).append(responseCode).append(" ").append(response.getStatusLine().getReasonPhrase()).toString();
                System.out.println(respuestaString);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("return true");
        LOGGER.log(Level.INFO, "zzz " + "FIN");
    }

    private static JsonObject buildNotificationMessage() {
        JsonObject jNotification = new JsonObject();
        jNotification.addProperty("title", "TITLE");
        jNotification.addProperty("body", "BODY");

        JsonObject jMessage = new JsonObject();
        jMessage.add("notification", jNotification);
        jMessage.addProperty("to", "e1ltVa_...CVOKg6D5");

        JsonObject jFcm = new JsonObject();
        jFcm.add("message", jMessage);

        return jFcm;
    }

问题出在哪里?我准备JSON的方式?

请帮忙。

2 个答案:

答案 0 :(得分:1)

https://fcm.googleapis.com/fcm/send的帖子使用Legacy HTTP Server Protocol。请求的JSON不使用message作为根。这是HTTP v1 API引入的。

进行以下更改:

private static JsonObject buildNotificationMessage() {
    JsonObject jNotification = new JsonObject();
    jNotification.addProperty("title", "TITLE");
    jNotification.addProperty("body", "BODY");

    JsonObject jMessage = new JsonObject();
    jMessage.add("notification", jNotification);
    jMessage.addProperty("to", "e1ltVa_...CVOKg6D5");

    //JsonObject jFcm = new JsonObject();  // <= NOT NEEDED
    //jFcm.add("message", jMessage);  // <= NOT NEEDED

    return jMessage;  // <= CHANGED
}

答案 1 :(得分:0)

您需要使用firebaseId而不是应用ID,重新安装您的应用,并在FirebaseRegistrationService中设置断点,此服务在应用程序生命周期中启动一次,并且唯一的工作就是获取firebase令牌ID。您收到错误400,因为您的令牌不正确。

car.Image