1

私のAndroidアプリケーションに通知を追加していますが、Javaコード経由で通知を送信できないようです。私は200のHTTPコードフォームを返信していますが、何も表示されません。FCMコンソールではなくJavaを使用してFirebase通知を送信

これはFirebaseコンソール自体で動作しますが、一度Javaコードを使用しようとすると、問題が発生します。

これは、次の

応答コードアウト

package actions.authentication; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import com.opensymphony.xwork2.Action; 

import edocs.actions.PublicAction; 
import net.sf.json.JSONObject; 

public class SendPushNotification extends PublicAction { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -8022560668279505764L; 

    // Method to send Notifications from server to client end. 
    public final static String AUTH_KEY_FCM = "SERVER_KEY_HERE"; 
    public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send"; 
    public final static String DEVICE_ID = "DEVICE_TOKEN_HERE"; 

    public String execute() { 
     String DeviceIdKey = DEVICE_ID; 
     String authKey = AUTH_KEY_FCM; 
     String FMCurl = API_URL_FCM; 

     try { 
      URL url = new URL(FMCurl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

      conn.setUseCaches(false); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 

      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Authorization", "key=" + authKey); 
      conn.setRequestProperty("Content-Type", "application/json"); 
      System.out.println(DeviceIdKey); 
      JSONObject data = new JSONObject(); 
      data.put("to", DeviceIdKey.trim()); 
      JSONObject info = new JSONObject(); 
      info.put("title", "FCM Notificatoin Title"); // Notification title 
      info.put("body", "Hello First Test notification"); // Notification body 
      data.put("data", info); 
      System.out.println(data.toString()); 
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
      wr.write(data.toString()); 
      wr.flush(); 
      wr.close(); 

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

      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      String inputLine; 
      StringBuffer response = new StringBuffer(); 

      while ((inputLine = in.readLine()) != null) { 
       response.append(inputLine); 
      } 
      in.close(); 

      return Action.SUCCESS; 
     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
     } 

     return Action.SUCCESS; 
    } 
} 

ラインSystem.out.println("Response Code : " + responseCode);プリント以下の私のコード:200

は誰にもこの問題を抱えていますか?任意の助けが大いに評価されるでしょう

答えて

2

あなたのコードは私のために働く。下の行を追加して、完全な応答ステータスを確認してください。出力をポストする。あなたはFirebaseがあなたのための通知を生成する場合

 while ((inputLine = in.readLine()) != null) { 
      response.append(inputLine); 
     } 
     in.close(); 

     System.out.println("Resonse: " + response); // <= ADD THIS 

また、bodytitlenotification、ないdataの性質である必要があります。詳細はthe documentationです。

 info.put("title", "FCM Notification Title"); // Notification title 
     info.put("body", "Hello First Test notification"); // Notification body 
     data.put("notification", info); // <= changed from "data" 
+0

はい!出来た!通知に情報をプッシュする必要があります。どうもありがとうございます! – Pooshonk

関連する問題