Last active
September 9, 2022 07:56
-
-
Save heart/14fff6077834f2e3cd15c6e50bc3d60d to your computer and use it in GitHub Desktop.
iOS send APNS Push via CURL and key.p8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# -- Configuration -- | |
DEVICE_TOKEN=XXXX439e2f0XXXXXXXXXXXXXXXXXXXXXXXXXX4728d9c688e8XXXXXX | |
AUTH_KEY="./AuthKey_xxxxxxxxx.p8" | |
AUTH_KEY_ID=XXXXXXXXX | |
TEAM_ID=375452VNBW | |
BUNDLE_ID=com.XXXXXXXXX.app | |
IS_PRODUCTION_SERVER=false | |
# -- Push Message -- | |
read -r -d '' payload <<-'EOF' | |
{ | |
"aps": { | |
"badge": 2, | |
"category": "mycategory", | |
"alert": { | |
"title": "my title", | |
"subtitle": "my subtitle", | |
"body": "my body text message" | |
} | |
}, | |
"custom": { | |
"mykey": "myvalue" | |
} | |
} | |
EOF | |
#======================================= | |
base64() { | |
openssl base64 -e -A | tr -- '+/' '-_' | tr -d = | |
} | |
sign() { | |
printf "$1" | openssl dgst -binary -sha256 -sign "$AUTH_KEY" | base64 | |
} | |
time=$(date +%s) | |
header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$AUTH_KEY_ID" | base64) | |
claims=$(printf '{ "iss": "%s", "iat": %d }' "$TEAM_ID" "$time" | base64) | |
jwt="$header.$claims.$(sign $header.$claims)" | |
if [ "$IS_PRODUCTION_SERVER" = "true" ]; then | |
endpoint=https://api.push.apple.com | |
else | |
endpoint=https://api.sandbox.push.apple.com | |
fi | |
curl --verbose \ | |
--header "content-type: application/json" \ | |
--header "authorization: bearer $jwt" \ | |
--header "apns-topic: $BUNDLE_ID" \ | |
--data "$payload" \ | |
$endpoint/3/device/$DEVICE_TOKEN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment