苹果推送通知与PHP脚本

时间:2012-04-23 08:24:14

标签: php iphone apple-push-notifications

我能够连接到apns并通过将值插入数据库进行检查我收到“消息传递”消息但是它没有显示任何通知....任何人都可以告诉我我错在哪里...

$deviceToken = '**********************';


$payload['aps'] = array('alert' => 'This is the alert text', 'badge'
=> 1, 'sound' => 'default'); $payload = json_encode($payload);

$passphrase = '***';


$apnsHost = 'gateway.sandbox.push.apple.com'; $apnsPort = 2195; $apnsCert = 'ck.pem';

$streamContext = stream_context_create(); stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert); stream_context_set_option($streamContext, 'ssl', 'passphrase',$passphrase );

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

if (!$apns)
    {
     $check = $errorString;      $query = "INSERT INTO tbl_checkData(friend_nickname) VALUES('$check') ";    $result=mysql_query($query);           } else{ 
     $check = "connected";   $query = "INSERT INTO tbl_checkData(friend_nickname) VALUES('$check') ";    $result=mysql_query($query);    }

$apnsMessage = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;

//chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;

$result=fwrite($apns, $apnsMessage);

if (!$result)
    {
     $check = 'Message not delivered' ;      $query = "INSERT INTO tbl_checkData(friend_nickname) VALUES('$check') ";    $result=mysql_query($query);       
            } else {     $check = 'Message delivered' ;      $query = "INSERT INTO tbl_checkData(friend_nickname) VALUES('$check') ";    $result=mysql_query($query);    
     }

enter code here

fclose($apns);

2 个答案:

答案 0 :(得分:3)

$ apnsHost ='gateway.sandbox.push.apple.com';

从此网址中删除沙箱,它将会运行... 我测试了它......

答案 1 :(得分:0)

确保: 1 - 您的应用未运行,因为推送通知仅用于在应用关闭时通知用途。 2 - 设备的设备令牌是正确的还要确保也为同一设备配置了推送通知证书。 3 - 确保您的设备已连接到互联网。

您可以查看此链接。

Push Notification