java - 无法从服务器向 FCM url 发送请求(相同的代码在本地电脑上工作)

标签 java firebase push-notification ssl-certificate firebase-cloud-messaging

无法从服务器向 FCM url 发送请求(相同的代码在本地电脑上运行)

以下是我在服务器日志中收到的错误

    javax.net.ssl.SSLException: Certificate for <fcm.googleapis.com> doesn't match any of the subject alternative names:  [*.googleapis.com, *.clients6.google.com, *.cloudendpointsapis.com, cloudendpointsapis.com, googleapis.com]at org.apache.http.conn.ssl.DefaultHostnameVerifier.matchDNSName(DefaultHostnameVerifier.java:157)

服务器通知发送代码(函数) 相同的代码在本地系统/电脑上完美运行

public int sendNotification(String registrationId, String title,String subtitle, String url ,String destitle, String description) throws JSONException {

    String uri = "https://fcm.googleapis.com/fcm/send";

    HttpClient client = HttpClientBuilder.create().build();

    try {
                                            HttpPost postRequest = new HttpPost(uri);
                                            postRequest.setHeader("Authorization","key=XXXXXXXXXXXXXXXXXXXXXXXXXXX");
                                            postRequest.setHeader("Content-type", "application/json");

                                            JSONObject json = new JSONObject();
                                            json.put("to",registrationId.trim());
                                            JSONObject data = new JSONObject();

                                            data.put("title", title);   // Notification title
                                            data.put("subtitle", subtitle); // Notification body
                                            data.put("destitle", destitle);
                                            data.put("url", url);
                                            data.put("description", description);


                                            json.put("data", data);

                                            StringEntity entity = new StringEntity(json.toString());
                                            postRequest.setEntity(entity);

        try {

            HttpResponse response = client.execute(postRequest);
            InputStreamReader inputStreamReader = new InputStreamReader(response.getEntity().getContent());
            BufferedReader rd = new BufferedReader(inputStreamReader);


            //System.out.println("NOTIFICATION RESPONSE ----->"+msg1+msg2);
            String line = "";
            while((line = rd.readLine()) != null)
            {
                System.out.println(line);
            }


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

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

    return 1;
}

最佳答案

添加主机名验证码.....创建httpclient对象

public HttpClient getHttpClient() {
    try {
                HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

                DefaultHttpClient client = new DefaultHttpClient();

                SchemeRegistry registry = new SchemeRegistry();
                SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
                socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
                registry.register(new Scheme("https", socketFactory, 443));
                SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
                DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

                // Set verifier
                HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

                // Example send http request

        return httpClient;
    } catch (Exception ex) {
        return HttpClientBuilder.create().build();
    }
}

关于java - 无法从服务器向 FCM url 发送请求(相同的代码在本地电脑上工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42945398/

相关文章:

java - 在java中计算S3对象(文件夹)的大小

mongodb - 创建基于用户偏好的通知系统

ios - 即使内容的 Storyboard已更新,也不会调用通知内容

java - 安卓 Firebase Firestore : Get data from deep in Firestore document

ios - 从 Parse.com 服务器在 iPhone 中有时没有收到推送通知

java - $unwind $push Mongo

java - 在 App Engine 上使用 RingoJS 读写数据

具有快速检索功能的 java 不断排序的列表

reactjs - React、Formik、React-select 和 Firebase - Formik 形式的 isMulti 数组

javascript - 如何从客户端安全地写入 Firebase?