简体   繁体   中英

Push Notification not working in Iphone

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM