Skip to content

Instantly share code, notes, and snippets.

@nfhipona
Last active January 20, 2021 06:17
Show Gist options
  • Save nfhipona/7bc31e32540ab3778f55dc3f9423947d to your computer and use it in GitHub Desktop.
Save nfhipona/7bc31e32540ab3778f55dc3f9423947d to your computer and use it in GitHub Desktop.
[APNs Authentication Key] APNS Test PUSH Script for debugging
#!/bin/bash
# https://firebase.googleblog.com/2017/01/debugging-firebase-cloud-messaging-on.html
# https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1
# https://developer.apple.com/videos/play/wwdc2016/724/
clear
echo
echo "------ Debug start ------"
echo
DEVICE_ID="Apple deviceToken format: %02.2hhx"
KEY_FILE="./key_file.p8"
AUTH_KEY_ID=
TEAM_ID=
BUNDLE_ID=
#ENDPOINT=https://api.push.apple.com
ENDPOINT=https://api.development.push.apple.com
read -r -d '' payload <<-'EOF'
{
"aps": {
"badge": 2,
"alert": {
"title": "Hello from APNs!",
"sound": "default",
"body": "APNs body content here"
},
"content-available": "1"
}
}
EOF
# --------------------------------------------------------------------------
base64() {
openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
}
sign() {
printf "$1"| openssl dgst -binary -sha256 -sign "$KEY_FILE" | 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)"
curl --verbose \
--header "content-type: application/json" \
--header "authorization: bearer $jwt" \
--header "apns-topic: $BUNDLE_ID" \
--data "${payload}" \
$ENDPOINT/3/device/$DEVICE_ID
echo
echo "------ Debug end ------"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment