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

Posted

技术标签:

【中文标题】无法从服务器向 FCM url 发送请求(相同的代码在本地 pc 上工作)【英文标题】:Unable to sent request to FCM url from server (same code is working from local pc) 【发布时间】:2017-03-22 07:35:37 【问题描述】:

无法从服务器向 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)

服务器通知发送代码(函数) 相同的代码在本地系统/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;

【问题讨论】:

你发现了吗? @grant ......是的,下面的答案完美......使用下面的 gerHttpclient() 函数创建 httpclient 对象 【参考方案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();
    

【讨论】:

SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER 违背了 SSL 的目的

以上是关于无法从服务器向 FCM url 发送请求(相同的代码在本地 pc 上工作)的主要内容,如果未能解决你的问题,请参考以下文章

PHP cURL无法向api发送POST请求

在应用程序被杀死后,使用 HTTP 请求通过 Firebase(FCM)向 iOS 设备发送推送通知 [重复]

是否需要在FCM中修剪设备?

向 FCM 服务器发送推送通知不起作用

向 FCM API 发送请求时收到无效的 JSON 有效负载

使用FCM从服务器发送推送通知