本覺得按照網上的一系列操做就能夠很是順利的完成,可是問題一個接一個的來。這個溝通確實是一門學問。
ios 和 php 聯合 推送功能,網上有好多文章,我以爲最好的是這一篇http://zxs19861202.iteye.com/blog/1532460,可是裏面的ios內容太讓我頭疼了,加上技術請假,我負責外包的交流,還要製做這個東西,真的是...啥都不說了。不過我大概的對這個流程有了一點了解。僅此記錄。 php
獲得deviceToken 要在證書裏面激活推送 , ios
1.證書須要激活推送功能 就能獲取到deviceToken, json
2.前段須要生成一個.pem文件 這個文件是 由2個文件 證書.p12和一個 .cer 共同生成的一個.pem app
3.證書 密碼 待定。 socket
php端比較簡單。代碼 測試
<?php ui
// 這裏是咱們上面獲得的deviceToken,直接複製過來(記得去掉空格) spa
$deviceToken = '740f4707bebcf74f 9b7c25d4 8e3358945f6aa01da5ddb387462c7eaf 61bb78ad'; code
// Put your private key's passphrase here: server
$passphrase = 'abc123456';
// Put your alert message here:
$message = 'My first push test!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
//這個爲正是的發佈地址
//$fp = stream_socket_client(「ssl://gateway.push.apple.com:2195「, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);
//這個是沙盒測試地址,發佈到appstore後記得修改哦
$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);
?>