2017-01-10 56 views
1

我試圖在Android應用程序中實現Firebase主題消息傳遞,並且試圖構建HTTP發佈請求,並且我收到的響應代碼爲400。我已經看過各種解決方案,但他們似乎都沒有幫助。HTTP發佈請求:錯誤400,Firebase主題消息傳遞

這裏就是我所說的AsyncTask的子類:

try{new FirebaseSendMessage().execute("Hello world");} 
       catch (Exception e) { 
        Log.d("Exception", e.toString()); 
       } 

這裏是我的異步任務類的子類。

class FirebaseSendMessage extends AsyncTask<String, Integer, Double> { 
private final static String USER_AGENT = "Mozilla/5.0"; 
private final static String AUTH_KEY = "<My firebase authorization key obtained from firebase>"; 

private Exception exception; 

protected Double doInBackground(String... params) { 
    try { 
     sendRequest(params); 
    } catch (Exception e) { 
     this.exception = e; 
    } 
    return null; 
} 

protected void onPostExecute(Long l) { 
    // TODO: check this.exception 
    // TODO: do something with the feed 
} 


public void sendRequest(String... params) { 
    try { 
     String urlString = "https://fcm.googleapis.com/fcm/send"; 
     URL url = new URL(urlString); 
     HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
     con.setDoOutput(true); 
     con.setRequestMethod("POST"); 
     con.setRequestProperty("Content-Type", "application/json"); 
     con.setRequestProperty("Authorization", "key=" + AUTH_KEY); 
     String postJsonData = "{\"to\": \"/topics/news\"\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\"}"; 
     con.setDoOutput(true); 

     DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
     wr.writeBytes(postJsonData); 
     wr.flush(); 
     wr.close(); 

     int responseCode = con.getResponseCode(); 
     System.out.println("POST Response Code :: " + responseCode); 

     if (responseCode == HttpURLConnection.HTTP_OK){ 
      System.out.println("succeeded"); 
     } 
     /*InputStream is = con.getInputStream(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     String line = null; 
     while ((line = br.readLine()) != null) { 
      System.out.println(line); 
     } 
     //con.disconnect();*/ 
    } 
    catch(IOException e){ 
     Log.d("exception thrown: ", e.toString()); 
    } 
} 

}

錯誤:I/System.out: POST Response Code :: 400

請讓我知道,如果有幫我調試需要額外的代碼片段。提前致謝!

+1

請不要在您的Android應用程序中包含此代碼。您正在將您的'AUTH_KEY'暴露給您應用的所有用戶,這意味着它可以(而且將會)被黑客發現,黑客可以用它代表您的應用發送消息。 –

+0

似乎firebase要求我這樣做,但是。有沒有解決方法? – kmindspark

+1

您應該使用應用程序服務器。或者從Firebase控制檯發送通知。將您的服務器密鑰嵌入到客戶端APK中*不是您想要的選擇。 –

回答

3

Error 400意味着無效JSON在您的請求:

Check that the JSON message is properly formatted and contains valid fields (for instance, making sure the right data type is passed in).

在你sendRequest,你錯過"news\"\"data\"和閉合支架(})之間加逗號(,):

String postJsonData = "{\"to\": \"/topics/news\"\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\"}"; 

看起來像這樣:

{"to": "/topics/news/""data":{"message":"...."} 

應該是:

String postJsonData = "{\"to\": \"/topics/news\", \"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\"}}"; 

這樣的JSON結構將是正確的:

{"to": "/topics/news/", 
"data":{"message":"..."} 
} 
+0

謝謝,這是錯誤。 – kmindspark

0

對於那些誰願意用認證密鑰的應用程序。

我建議您通過應用程序的SHA-1手動加密密鑰,並在運行時使用SHA-1代碼對密鑰進行解密。