且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用PHP脚本苹果推送通知

更新时间:2023-02-16 22:50:20

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

从这个URL删除沙箱,它会运行...
我测试...

I am able to connected to the apns and by inserting the values to database for just check i am getting "message delivered" message but it's not showing any notification....can anybody tell me where i am mistaken...

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

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

remove sandbox from this url and it will run... i have tested it...