PHP Sending Push Notifications Failed to connect: 111 Connection refused
Asked Answered
D

3

6

I am trying to setup push notifications, I got my certificates from apple developer, unabled pushed notifications on my ios app and I have a PHP script that is suppose to send a push notification. My hosting provider whitelisted the port 2195 outbound for my account. But push notifications are still not working, I get this error

Failed to connect: 111 Connection refused

I am using Development Certificates and they are enabled in my apple developer account.

<?php 

class PushNotifications {

    private static $passphrase = 'PASSPHRASE';

    public function iOS($data, $devicetoken) {

        $deviceToken = $devicetoken;

        $ctx = stream_context_create();

        stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);

        $fp = stream_socket_client(
            'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        if (!$fp)
        {
            exit("Failed to connect: $err $errstr" . PHP_EOL);
        }

        $body['aps'] = array('alert' => array('title' => $data['mtitle'], 'body' => $data['mdesc'],),'sound' => 'default');

        $payload = json_encode($body);

        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

        $result = fwrite($fp, $msg, strlen($msg));

        fclose($fp);

        if (!$result)
            return 'Message not delivered' . PHP_EOL;
        else
            return 'Message successfully delivered' . PHP_EOL;

    }

    private function useCurl(&$model, $url, $headers, $fields = null) {

        $ch = curl_init();
        if ($url) {
            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_VERIFYPEER, false);
            if ($fields) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            }
            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('Curl failed: ' . curl_error($ch));
            }

            curl_close($ch);

            return $result;
        }
    }

}
?>

Can anyone help?

Doering answered 25/8, 2018 at 7:10 Comment(2)
Is this for production or development?Sayles
This is for developmentDoering
A
1

Make sure you're not still getting blocked (run this from your php host):

telnet gateway.sandbox.push.apple.com 2195

If that gets a connection refused, then the problem is still with your hosting provider or someone between you and APNS.

Or this, if you don't have shell access:

fsockopen('gateway.sandbox.push-apple.com.akadns.net', 2195);
Allonym answered 3/9, 2018 at 8:59 Comment(0)
E
0

Try this hopefully, it's working for you.

Message Data

$pushnoti = array();
$pushnoti['type'] = 'message_notification';
$pushnoti['body'] = array( 'message' => "Your Order id Completed");
send_notification_iphone ($deviceToken, $pushnoti);

Notification Function

function send_notification_iphone( $deviceToken, $data) {

    // Put your private key's passphrase here:
    $passphrase = self::$passphrase;
    $pem = 'ck.pem';
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
    // Open a connection to the APNS server
    $fp = stream_socket_client(
        'ssl://gateway.sandbox.push.apple.com:2195', $err,
        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);
    $message['body'] = $data['body'];
    $body['aps'] = array( 'alert' => $data['body']['message'], 'body' => $data['body'], 'type'=> $data['type'] );
    $body['badge'] =3;
    $body['sound'] ='cat.caf';
    // Encode the payload as JSON
    $payload = json_encode($body);
    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));
    if (!$result)
        echo 'Message not delivered' . PHP_EOL;
    else
        // Close the connection to the server
        fclose($fp);
}
Ejective answered 30/8, 2018 at 8:48 Comment(0)
S
0

I also got this error. Following things you have to remember:

  1. Path of ios push cert file should be correct. Check on online portals using device token and push cert.
  2. Your server is enabled for 2195 port. Check on terminal by using telnet command.
  3. Check ssl supported by your php version if using

    ssl://gateway.sandbox.push.apple.com

    and tls if using

    tls://gateway.sandbox.push.apple.com

  4. The data you are sending in the push or token etc should be equal or less than the specified bytes mentioned in the apple doc. link is mentioned below.

You can also use api get reference for sending push, Development server:

api.development.push.apple.com:443

Production server:

api.push.apple.com:443

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1

Scandium answered 11/4, 2019 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.