2017-07-20 17 views
0

firebaseに接続してアンドロイドアプリにプッシュ通知を送信しようとしています。 Java Server側で次のコードを記述しました。しかし、私は常に接続例外をタイムアウトしています。firebase接続タイムアウトのJavaサーバー側接続

final String apiKey="AAAAeEpqP-w:APA91bFulyT23Km-onTNr_q5yEz4uoOaM8KdE4LMyIoz6kWlk3pJSHirDJBSiqESRXKiGa-Z_tBfpXA6naaaTXxcFFxAnaSkMTPVVOMswyJ0bhhdpwlo-92HXgxRMsHV6Y8bNaHX7tMd"; 
int i=0; 
try { 

URL url = new URL("https://fcm.googleapis.com/fcm/send"); 
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
System.out.println("after connection open"); 
//conn.setDoInput(true); 
conn.setRequestMethod("POST"); 
conn.setRequestProperty("Content-Type", "application/json"); 
conn.setRequestProperty("Authorization", "key=" + apiKey); 
conn.setDoOutput(true); 
JSONObject json = new JSONObject(); 
    json.put("to", "device-token"); 
    JSONObject info = new JSONObject(); 
    info.put("title", "notification title"); // Notification title 
    info.put("body", "message body"); 
    json.put("notification", info); 
    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("Output from Server .... \n"); 
    while ((output = br.readLine()) != null) { 
     System.out.println(output); 
    } 
    // result = CommonConstants.SUCCESS; 
System.out.println("after closed out put stream"); 
int responseCode = conn.getResponseCode(); 

// System.out.println("Post parameters : " + input); 
System.out.println("Response Code : " + responseCode); 


} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 

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

常にセッションタイムアウトエラーを示します。 アプリに通知を送信するのは正しい方法ですか? 何か助けていただければ幸いです。

EDIT: - 私はデバイストークンなどで休憩クライアントから他のものを呼び出していますが、うまくいきます。私のアプリで通知があります。しかし、Javaコードを使って送信すると、でる。

答えて

0

サーバの設定に役立つ公式のFirebaseのマニュアルです。 https://firebase.google.com/docs/cloud-messaging/server

あなたのコードでは、私はjson.put("to", "device-token");に気付きました。この例ではプレースホルダとして使用していると仮定しています。そうでない場合は、あなたの要求で{ "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." }のようなデバイストークンを送る必要があります。

+0

私は前述のコードを追加しました。私はAndorid側のJavaコードで上記のコードを追加しました。時々通知を受けることはありますが、必ずしもそうではありません。開発モードでチェックしています。今私のメッセージ受信コードは、私はメッセージの受信に入れたログのトレースを取得します。しかし、通知は電話に来ていません。 – Mahi

関連する問題