iOS遠端推送測試

2021-07-04 14:05:32 字數 2652 閱讀 2736

一、開發測試

這裡需要用到終端,建議將aps_development.cer、pushme.php、push.p12放到乙個資料夾中,假設名字是iosdevelop,同時用cd命令到這個iosdevelop資料夾中。用到的命令如下:

1,openssl x509 -in aps_development.cer -inform der -out pushchatcert.pem

2,openssl pkcs12 -nocerts -out pushchatkey.pem -in push.p12

3,cat pushchatcert.pem pushchatkey.pem > ck.pem

總共有3個,這裡就不解釋了,大家可以看本文後面的鏈結教程。

上面3個語句執行完成後,接下來可以執行

php pushme.php

這裡用的是php的推送測試,pushme.php的內容如下:

<?php

// put your device token here (without spaces):

$devicetoken = '你的裝置的device token,注意不是udid';

// put your private key's passphrase here:密語

$passphrase = '123456';

// put your alert message here:

$message = '測試推送\n訊息,這裡是可以用\n換行的';

$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(

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

?>

執行完成php pushme.php後,終端會顯示如下資訊:

connected to apns

message successfully delivered

稍後你的裝置就會收到你傳送的推送訊息。

二、發布測試

由於蘋果的審核週期很長,不能每次都用加急審核,所以最好在提交前做一次測試,看看伺服器的推送是否正確。發布測試也類似於開發測試,也是要用到那3個語句。

1,openssl x509 -in aps_production.cer -inform der -out pushchatcert.pem

2,openssl pkcs12 -nocerts -out pushchatkey.pem -in push.p12

3,cat pushchatcert.pem pushchatkey.pem > ck.pem

開發的時候一般我們都是用xcode直接灌到裝置裡,這個時候development證書是可以推送成功的。 但是production證書卻推送不了,不過蘋果給我們提供了ad hoc測試方式,它使用的是發布證書。如果你不知道ad hoc模式可以搜一下,很簡單的,這裡有乙個關鍵的地方,development和production得到的token的不一樣的,使用ad hoc方式裝到裝置上得到的就是production token 所以你在用ad hoc方式。測試的要使用production token,如果使用ad hoc方式測試沒問題,這個發布也就沒問題了。

最後執行php pushme.php,就可以了。

iOS 遠端推送

ios應用的 之需做2件事情 1.在程式啟動時,註冊遠端通知服務,說白了 就是發訊息給蘋果,詢問 我的最新devicetoken是多少?注意 第一次註冊時,需要使用者授權,也就是我們常見的 應用 想要給您傳送推送通知 具體的 如下 註冊遠端通知服務 uiremotenotificationtypes...

iOS的遠端推送

ios訊息推送的工作機制可以簡單的用下圖來概括 上圖可以分為三個階段 第一階段 應用程式把要傳送的訊息 目的iphone的標識打包,發給apns。第二階段 apns在自身的已註冊push服務的iphone列表中,查詢有相應標識的iphone,並把訊息傳送到iphone。第三階段 iphone把發來的...

iOS PushMeBaby 遠端推送測試

pushmebaby 是一款遠端推送測試的第三方。2 執行之後可能會報錯 users dengzemiao downloads pushmebaby master classes iosock.h 52 10 coreservices frameworks carboncore.framework ...