工具項目地址
github
https://github.com/wardenlym/apns2-test
工具安裝
參考項目描述編譯安裝即可工具(很簡單,直接make就可以)
如有報錯,根據錯誤提示解決即可。
我編譯時遇到autoconf版本低(2.63)導致編譯失敗的問題,后面升級下autoconf就好了,我是升級到2.68
工具使用介紹
用法
最簡用法,只有2個必須參數: 證書,要推送設備的token。
./apns2-test -cert <cert.pem> -token
然后設備就會收到一條 "apns2 test." 的默認測試信息。
可選參數
-dev 開發環境,默認推生產環境
-debug 詳細的日志了連接過程和HTTP請求過程
-topic 不填的話,從證書里面取UID,默認就是應用的bundle-id
-message 自定義一個消息內容,比較常用,替換的是alert字段
example: -message "abc test."
-payload 如果你對payload中的字段需要詳細的指定,這個選項跟-message沖突,不應該同時設置(廢話。。。),如果都設置,以-payload為准。
example: -payload '{"aps":{"alert":"payload test.","sound":"default"}}'
還有些沒什么太大用的選項,但也留出來方便些:
-uri 如果你有特別的代理,可以用來替換蘋果默認的域名,這個如果設置了,默認覆蓋生產開發選項。你指定哪個地址就推哪個。
-port 如果你要設定端口,默認蘋果端口是2197
-prefix 這個一般不用改吧。協議里的header前綴 /3/device/
-pkey 如果需要指定一個私鑰文件,正常來說,證書與私鑰都在一個pem文件里。默認都從pem里面讀取
效果
debug效果
[root@local-vm apns2-test]# ./apns2-test -cert /tmp/test_dev.pem -token 167f86c48b3418cbe45ff3bfe51f4d504df456d1110a77de0dfb5576294b758e -debug -dev
6 -dev
notBefore : 190225060336Z
notAfter : 200225060336Z
UID = com.test [23]
apns2-test version: 0.1.1
nghttp2 version: 1.9.0-DEV
tls/ssl version: TLSv1.2
ns looking up ...
connecting to : 17.188.165.219
socket connect ok: fd=3, host: api.development.push.apple.com:2197
Enter PEM pass phrase:
ssl allocation ok
ssl handshaking ...
Connected with encryption: ECDHE-RSA-AES256-GCM-SHA384
ssl handshake ok
[INFO] Stream ID = 1
[INFO] C ----------------------------> S (HEADERS)
:method: POST
:path: /3/device/167f86c48b3418cbe45ff3bfe51f4d504df456d1110a77de0dfb5576294b758e
apns-topic: com.test
[INFO] C ----------------------------> S (DATA post body)
{"aps":{"alert":"apns2 test.","sound":"default"}}
[INFO] C <---------------------------- S (HEADERS begin)
:status: 200
apns-id: D63E10AF-D391-9958-6C70-92CC5759204B
[INFO] C <---------------------------- S (HEADERS end)
[INFO] C ----------------------------> S (GOAWAY)
over.
debug消息如上,能看到
- {"aps":{"alert":"apns2 test.","sound":"default"}}
推送的消息內容,可以用message參數指定 - :status: 200 (推送結果,對照如下)
Status code | Description |
---|---|
200 | Success |
400 | Bad request |
403 | There was an error with the certificate or with the provider authentication token |
405 | The request used a bad :method value. Only POST requests are supported. |
410 | The device token is no longer active for the topic. |
413 | The notification payload was too large. |
429 | The server received too many requests for the same device token. |
500 | Internal server error |
503 | The server is shutting down and unavailable. |
結束
項目中有時排查推送問題,自己單個推送調試排查問題還是很不錯的