NguyenChanhTiep / Notes

0 stars 0 forks source link

Send APNs with cURL #1

Open NguyenChanhTiep opened 2 years ago

NguyenChanhTiep commented 2 years ago

Sử dụng P12

Export file certificates p12

Trong máy phải cài đặt certificates bao gồm cả private key Export .p12 với điền mật khẩu

Mở terminal tại folder chứa file cert vừa export và nhập theo format

curl -v \
-d '{PLAYLOAD}' \
--http2 \
--cert-type P12 --cert {TEN_FILE_CERT}.p12:{MAT_KHAU_FILE_CERT} \
https://api.sandbox.push.apple.com/3/device/{DEVICE_TOKEN} 

Ví dụ

curl -v \
-d '{"aps":{"alert":"hello"}}' \
--http2 \
--cert-type P12 --cert Certificates.p12:12345 \
https://api.sandbox.push.apple.com/3/device/d440ab865b5a249bf8990b12d6e2635d9d0178b9d52eb1152953765ad8a5a7b4

Sử dụng file PEM

Export file certificates .p12 và file .cer

Trong máy phải cài đặt certificates bao gồm cả private key Export .p12 với private key (điền mật khẩu) Export .cer không bao gồm private key (khi export sẽ không yêu cầu cấp mật khẩu).

Tạo file PEM

openssl x509 -in certificates.cer -inform der -out certificates_cer.pem
openssl pkcs12 -in certificates.p12 -out certificates_p12.pem
Ở bước này: sẽ nhập mật khẩu file p12, sau đó mật khẩu file pem.
cat certificates_cer.pem certificates_p12.pem > certificates.pem

Mở terminal tại folder chứa file cert vừa export và nhập theo format

curl -v \
-d '{PLAYLOAD}' \
--http2 \
--cert certificates.pem:{MAT_KHAU_FILE_PEM} \
https://api.sandbox.push.apple.com/3/device/{DEVICE_TOKEN} 

Ví dụ

curl -v \
-d '{"aps":{"alert":"hello"}}' \
--http2 \
--cert certificates.pem:12345 \
https://api.sandbox.push.apple.com/3/device/d440ab865b5a249bf8990b12d6e2635d9d0178b9d52eb1152953765ad8a5a7b4

Sử dụng P8

Yêu cầu

File p8 Team ID Key ID

Generate JSON Web Token (JWT) with ES256 algorithm (Các ngôn ngữ Back End đều có hỗ trợ tạo JWT) Có thể dùng thư viện viết bằng swift này để generate token: https://github.com/ethanhuang13/CupertinoJWT

Mở terminal và nhập theo format

curl -v \
-d '{PLAYLOAD}' \
-H "apns-topic: {BUNDLE_ID}.voip" \
-H "authorization: bearer {TOKEN}" \
--http2 \
https://api.sandbox.push.apple.com/3/device/{DEVICE_TOKEN} 

Ví dụ

curl -v \
-d '{"aps":{"alert":"hello"}}' \
-H "apns-topic: com.blaone.medinet.voip" \
-H "authorization: bearer eyJhbGciOiJFUzI1NiIsImtpZCI6IjNLOTdBNllVNTkifQ.eyJpc3MiOiI0VzhVTjRXUjRIIiwiaWF0IjoxNjUwOTgwOTAyLCJleHAiOjE2NTA5ODQ1MDJ9.SrJnl-dFMBTpWvAIVtns80Bl12S0SXiOSF1rJvoBrT8ljA6-MT3gyF9LAEnb0nujXBwrsI-OpewywVkvZkXpng" \
--http2 \
https://api.sandbox.push.apple.com/3/device/d440ab865b5a249bf8990b12d6e2635d9d0178b9d52eb1152953765ad8a5a7b4
NguyenChanhTiep commented 2 years ago

Send notification request to APNs.pptx