使用php进行IOS推送

时间:2015-08-27 11:16:07

标签: php ios

我已经为ios创建了pushnotification。以下编码我从网站获得。设备令牌来自我已更改的Apple设备。

<?php
$deviceToken = '0f744707bebcf74f9b7c25d48e3358945fxxx01da5ddb387462c7eaf61bbad78';
$msg_push="hi there";
$deviceToken = str_replace(" ", "", $deviceToken);
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = $msg_push;
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');//'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$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);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
?>

我已将pushcert.pem文件包含在同一文件夹中。知道我什么时候在服务器上运行这个页面我得到msg“连接成功传送APNS消息”。但我没有在我的苹果设备中收到通知消息。这段代码中的任何错误都是如此。这有什么不对?

1 个答案:

答案 0 :(得分:-2)

可能是因为您的应用处于生产环境中,因此您必须从&#39; ssl://gateway.sandbox.push.apple.com中删除沙箱:2195&#39;

可能是因为苹果帐户中没有为特定的捆绑包标识符启用推送通知。也可能是因为推送通知的方法未在应用程序中定义。