iOS開發(fā)——APNs推送

P12文件

訪問 蘋果開發(fā)者賬戶,進入帳號首頁,選擇Certificates, Identifiers & Profiles

image.png

在 Certificates, Identifiers & Profiles 中,點擊 App IDs 創(chuàng)建應(yīng)用的AppID。


image.png

此處需要指定完整的 Bundle ID,不能使用通配符星號,擁有通配符appID是無法正常使用APNs推送服務(wù)。


image.png

為 App 開啟 Push Notification 功能。如果是已經(jīng)創(chuàng)建的 App ID 也可以通過設(shè)置開啟 Push Notification 功能。


image.png

如果你之前沒有創(chuàng)建過 Push 證書或者是要重新創(chuàng)建一個新的,請在證書列表下面新建。 APNs 證書有開發(fā)(Development)和生產(chǎn)(Production)兩種。開發(fā)證書用于開發(fā)調(diào)試使用;生產(chǎn)證書既能用于開發(fā)也可以產(chǎn)品發(fā)布,但是建議開發(fā)和發(fā)布分開以免出現(xiàn)推送事故。


image.png

點擊 "Continue", 之后選擇該證書準備關(guān)聯(lián)的 AppID。


image.png

上傳 CSR 文件,


image.png

打開系統(tǒng)自帶的 鑰匙串KeychainAccess 創(chuàng)建 CSR 文件

image.png

填寫“用戶郵箱”和“常用名稱” ,并選擇“存儲到磁盤”,證書文件后綴為 .certSigningRequest


image.png

上傳剛剛生成的后綴為 .certSigningRequest 的文件。
生成證書成功后,點擊 “Download” 按鈕把證書下載下來,是后綴為 .cer 的文件

image.png

在鑰匙串列表中“登錄”——“我的證書”,找到剛才下載的證書,并導(dǎo)出為 .p12 文件


image.png

PEM文件

p12文件生成pem的腳本

#gen
openssl pkcs12 -clcerts -nokeys -out cert.pem -in Certificates.p12
openssl pkcs12 -nocerts -out key.pem -in Certificates.p12
openssl rsa -in key.pem -out key.unencrypted.pem
cat cert.pem key.unencrypted.pem > ck.pem

#clear
rm cert.pem
rm key.pem
rm key.unencrypted.pem

推送測試

將生成的pem文件和PHP在一個文件夾下 修改發(fā)送的token地址

<?php
//Usage: php pushMe.php (sandbox 1 password 123456 token xxx message 'push test')

//Params
$params = array(
    'sandbox' => 1,
    'token' => '9bf41ed10a47ac3efcea9bb4c670f557dc60366d815192b49dbc471b01617ca5',
    'password' => '123456',
    'message' =>'baxiang test!',
    );

if ($argc > 1){
    for ($i = 1; $i < $argc; $i+=2){
        $key = $argv[$i];
        $value = '';
        if ($i+1 < $argc) $value = $argv[$i+1];
        $params[$key] = $value;
    }
}
// print_r($params);

// Put your device token here (without spaces):
$deviceToken = $params['token'];

// Put your private key's passphrase here:
$passphrase = $params['password'];  //password

// Put your alert message here:
$message = $params['message'];

$host = 'ssl://gateway.push.apple.com:2195';
if ($params['sandbox'] == 1) $host = 'ssl://gateway.sandbox.push.apple.com:2195';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
// Open a connection to the APNS db2_server_info(connection)
$fp = stream_socket_client(
    $host, $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'
    );
$body['msgType'] = 'ORDER';

// 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 to '.$host . PHP_EOL;
else
    echo 'Message successfully delivered to '.$host . PHP_EOL;

// Close the connection to the server
fclose($fp);

測試發(fā)送PHP

php pushTest.php
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容