Php 推送通知在Iphone中不起作用

Php 推送通知在Iphone中不起作用,php,ios,iphone,apple-push-notifications,Php,Ios,Iphone,Apple Push Notifications,我使用了这个php代码 $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 = '*

我使用了这个php代码

$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);
当我从服务器向设备发送推送通知时,我得到了以下错误

我尝试更改ck.pem文件以获得解决方案

但它不起作用。因此,我认为ck.pemCertificate文件没有问题

警告:流\u套接字\u客户端:无法连接到 ssl://gateway.sandbox.push.apple.com:2195 在中拒绝连接 /第55行的home/sunstate/public_html/sunstate_api/gcm_message.php 连接失败:111连接被拒绝


通过硬编码您的设备令牌,尝试下面的代码

<?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);
?>
在.php文件中复制此代码并将其上载到服务器,然后通过添加实际的设备令牌从浏览器运行此文件

<?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);
?>

此外,如果您使用的是任何托管服务器,则需要在该服务器上安装SSL,并要求他们打开与提供商阻止的通知相关的端口。

Hi,您是否正在测试开发或发布?另请参阅。您应该确保三件事:1 TLS 1.0或以上;2服务器Nam指示;3委托.net证书颁发机构2048根。