无法从服务器向 FCM url 发送请求(本地 PC 正在处理相同的代码)



无法从服务器向FCM网址发送请求(本地PC上相同的代码正在工作(

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

    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)

服务器通知发送代码(功能(相同的代码在本地系统/PC 上完美运行

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

相关内容

  • 没有找到相关文章

最新更新