推送通知在Iphone中无效

时间:2015-04-18 13:09:59

标签: 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 = '******';
    $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.pem(Certificate)文件存在问题。

  

警告:stream_socket_client():无法连接   ssl://gateway.sandbox.push.apple.com:2195(拒绝连接)   第55行/home/sunstate/public_html/sunstate_api/gcm_message.php   连接失败:111拒绝连接

1 个答案:

答案 0 :(得分:0)

通过对设备令牌进行硬编码来尝试以下代码。

<?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文件中并将其上传到您的服务器,并通过添加实际设备令牌从浏览器运行此文件。

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