0

I'm using FCM push notifications for messaging everything woks fine but sometimes when I want to send push notification to someone I got

Curl failed: Failed to connect to fcm.googleapis.com port 443: Connection timed out

I have searched almost everything. here appears similar issue, but my port is open Sending notification message to android device curl error

also my network is not under the proxy.

Is it possible that the issue were causing from frequently calling of firebase send notification? Does it have some restrictions?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Jemo Mgebrishvili
  • 5,187
  • 7
  • 38
  • 63

1 Answers1

0

It could be one of these reasons.

  1. Server does not have internet access
  2. If you are trying it on localhost (your own PC) it wont work. Please test it on your server.
  3. You are not sending your request properly (below is the code for your reference PHP).

    function sendFCMPushNotification(){
    $server_key = //"enter your server key here";
    $mobile_tokens = array("token 1", "token 2", "token n");
    $n_data = array("title"=>"Test Notification", "attribute"=>"3999", "parameter 2"=>"value");
    
        $json_data = [
            "to" => implode($mobile_tokens),
            "notification" => [
                "title" => "FCM PUSH",
                "body" => "Text to be shown under the title",
                "click_action" => "MAIN_ACTIVITY" // activity to be opened in application, remove this if you dont require one.
            ],
            "data" => $n_data
        ];
    
        $data = json_encode($json_data);
    
        $url = 'https://fcm.googleapis.com/fcm/send';
    
        $headers = array(
            'Content-Type:application/json',
            'Authorization:key='.$server_key
        );
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        //print($result);
    
        if ($result === FALSE) {
            print_r('Oops! FCM Send Error: ' . curl_error($ch));
        }else{
            print_r('WoW! FCM SENT SUCCESSFULLY');
        }
        curl_close($ch);
    }
    

    }

Ali Haider
  • 92
  • 2