0

When I run the following script local, it sends push notifications to any device but when I run it in server I have this error:

111 Connection refused

Php code:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class PushNotification extends CI_Controller
{

function __construct()
{
    parent::__construct();
    $this->load->helper('url');
}

function index()
{

    $deviceToken = '64X';

    $message = 'My first push notification!'.'<ahref="http://google.com">Google.com</a>';

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev.pem');
    //stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

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

    if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);
    echo 'Connected to APNS' . PHP_EOL ;

    $body['aps'] = array(
        'alert' => $message,
        '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));

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

    fclose($fp);

}

}//end of class
Kerberos
  • 4,036
  • 3
  • 36
  • 55

1 Answers1

0

Answer : I sent a message to my host to open outgoing port (outbound) , they did that and this code working correctly :)