简体   繁体   English

Java服务器端连接与Firebase连接超时

[英]Java server side connection with firebase connection timeout

I am trying to connect to firebase to send push notification to an android app. 我正在尝试连接到Firebase以将推送通知发送到android应用。 I have written following code in Java Server side. 我已经在Java Server端编写了以下代码。 But I am getting connection timed out exception always. 但是我总是得到连接超时异常。

 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();
 }
}

It always shows session timeout error. 它始终显示会话超时错误。 Is it a correct way to send notifications to app. 这是向应用程序发送通知的正确方法。 Any help is appreciated. 任何帮助表示赞赏。

EDIT :- I have called the same url with device token and other things from rest client and it works well.I got a notification in my app.But when i send it thru Java code at that time it shows connection timed out. 编辑:-我已经从其他客户端使用设备令牌和其他名称调用了相同的URL,并且运行良好。我在我的应用中收到了通知。但是当我通过Java代码发送该URL时,它表明连接超时。

Here's the official Firebase documentation which can help you set up the server. 这是Firebase的官方文档,可以帮助您设置服务器。 https://firebase.google.com/docs/cloud-messaging/server https://firebase.google.com/docs/cloud-messaging/server

In your code I noticed json.put("to", "device-token"); 在您的代码中,我注意到json.put("to", "device-token"); and I am assuming that you are using it as a placeholder only for the example. 并且我假设您仅将其用作示例的占位符。 If not, in your request you need to send a device-token like { "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." } . 如果不是,则在您的请求中,您需要发送设备令牌,例如{ "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM