Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Firebase错误400发送HttpPost消息_Java_Android_Firebase_Http Post_Firebase Cloud Messaging - Fatal编程技术网

Java Firebase错误400发送HttpPost消息

Java Firebase错误400发送HttpPost消息,java,android,firebase,http-post,firebase-cloud-messaging,Java,Android,Firebase,Http Post,Firebase Cloud Messaging,我试图从Java服务器向特定的Android设备发送消息,但它返回了一个错误:400错误请求 应用程序的键是正常的,因为在Firebase控制台中发现之前,我一直在尝试使用其他错误的键 代码是: public static void sendMensaje(){ LOGGER.log(Level.INFO, "zzz Empieza"); String respuestaString = new String("OK \n"); try

我试图从
Java
服务器向特定的
Android
设备发送消息,但它返回了一个错误:
400错误请求

应用程序的键是正常的,因为在
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的方式


请提供帮助。

您需要使用firebaseId而不是应用程序id,重新安装应用程序,并在FirebaseRegistrationService中设置断点,此服务在应用程序生命周期中启动一次,唯一的任务是获取firebase令牌id。由于您的令牌不正确,因此出现错误400

public class FirebaseCloudRegistrationService extends FirebaseInstanceIdService {
    String TAG = "FirebaseNotification";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

  breakpoint -->  storeRegistrationId(this, refreshedToken);
    }
}

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

public class FirebaseCloudRegistrationService extends FirebaseInstanceIdService {
    String TAG = "FirebaseNotification";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

  breakpoint -->  storeRegistrationId(this, refreshedToken);
    }
}

发布到
https://fcm.googleapis.com/fcm/send
使用。请求的JSON不使用
消息
作为根。这一点是随着时间的推移而引入的

进行以下更改:

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
}
私有静态JsonObject buildNotificationMessage(){
JsonObject jNotification=新的JsonObject();
jNotification.addProperty(“所有权”、“所有权”);
jNotification.addProperty(“主体”、“主体”);
JsonObject jMessage=新的JsonObject();
添加(“通知”,jNotification);
jMessage.addProperty(“to”,“e1ltVa_uu…CVOKg6D5”);

//JsonObject jFcm=新的JsonObject();//发布到
https://fcm.googleapis.com/fcm/send
使用。请求的JSON不使用
消息
作为根。这是在中引入的

进行以下更改:

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
}
私有静态JsonObject buildNotificationMessage(){
JsonObject jNotification=新的JsonObject();
jNotification.addProperty(“所有权”、“所有权”);
jNotification.addProperty(“主体”、“主体”);
JsonObject jMessage=新的JsonObject();
添加(“通知”,jNotification);
jMessage.addProperty(“to”,“e1ltVa_uu…CVOKg6D5”);

//JsonObject jFcm=新的JsonObject();//那是安卓设备的令牌,不是吗?我是这样拿的,并将其编码为测试这是个坏主意,每次你重新安装/清除缓存你的应用程序时,令牌更改,而是将其存储在SharedReferencea中。另外,如果你在多个设备上测试,它会更改安卓设备的令牌,不是吗?我是这样拿的,并将其编码为测试这是个坏主意,每次你重新安装/清除缓存你的应用时,令牌都会改变,而不是将它存储在SharedReferencea中。如果你在多个设备上进行测试,令牌也会改变