0

I have used this php code

$query = "SELECT * FROM iphone_register";
$result_iphone = mysql_query($query);
$fetres = mysql_num_rows($result_iphone);

while ($rows = mysql_fetch_object($result_iphone)) {

    $deviceToken = $rows->device_id;

    $passphrase = '******';
    $message = 'New Push Notification!';

    $badge = 1;

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $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' => 'new notification is arrived',
        'body'=> $message,
        'badge' => $badge,
        'sound' => 'newMessage.wav'
    );

    $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 'Error, notification not sent' . PHP_EOL;
} else {
   echo 'notification sent!' . PHP_EOL;
}

fclose($fp);

When I send push notification to device from server then I got following error.

I try to change the ck.pem file for solution.

but it's not working. So I don't think so it has problem with ck.pem(Certificate) file.

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/sunstate/public_html/sunstate_api/gcm_message.php on line 55 Failed to connect: 111 Connection refused

eloibm
  • 899
  • 2
  • 11
  • 27
hardramoliya
  • 163
  • 1
  • 2
  • 13
  • Hi, are you testing for development or release ? – vivekDas Apr 18 '15 at 17:43
  • Also see [“verify error:num=20” when connecting to gateway.sandbox.push.apple.com](http://stackoverflow.com/a/23351633/608639). You should ensure three things: (1) TLS 1.0 or above; (2) Server Nam Indication; (3) *Entrust.net Certification Authority (2048)* root. – jww Nov 14 '16 at 06:44

1 Answers1

0

Try like below code by hard coding you device token.

<?php
$deviceToken = 'yourdevicetoken'; // masked for security reason

// Passphrase for the private key (ck.pem file)
$pass = 'xxxxx';

// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'Message received from BRL server';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
//$deviceToken = $_GET['deviceToken'];

// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;

/* End of Configurable Items */

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_dev.pem');

// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);

$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr,60,STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
    print "Failed to connect $err $errstr\n";
    return;
}
else {
    print "Connection OK\n";
}

$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>

Copy this code in .php file and upload it to your server and run this file from browser by adding actual device token.

Also if you are using any hosting server then you need to install SSL on that server and ask them to open port related to notification which was blocked by provider.

Nilesh Kikani
  • 2,628
  • 21
  • 37