Skip to content

Instantly share code, notes, and snippets.

@ben-xD
Created September 22, 2021 04:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ben-xD/14e4ab65d4e85faa9b6214e0f8f049af to your computer and use it in GitHub Desktop.
Save ben-xD/14e4ab65d4e85faa9b6214e0f8f049af to your computer and use it in GitHub Desktop.
Direct APNs Push Notifications
#!/usr/bin/env bash
# your team id located in the developer portal
TEAMID="replace"
# your key id located in the developer portal
KEYID="replace"
# the path to the key file you have stored in a safe place
SECRET="/Users/.apple/AuthKey_XXXXXXXXX.p8"
# your app bundle ID
BUNDLEID="com.example.app"
# your device push token
DEVICETOKEN="your_device_token"
# make input base64 url safe
function base64URLSafe {
openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
}
# sign input with you key file
function sign {
printf "$1"| openssl dgst -binary -sha256 -sign "$SECRET" | base64URLSafe
}
# now
time=$(date +%s)
# your header section
#
# e.g.
# {
# "alg" : "ES256",
# "kid" : "ABC123DEFG"
# }
header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$KEYID" | base64URLSafe)
# your claims section
#
# e.g.
# {
# "iss": "DEF123GHIJ",
# "iat": 1437179036
# }
claims=$(printf '{ "iss": "%s", "iat": %d }' "$TEAMID" "$time" | base64URLSafe)
# concatenate your header, your claim and a signed version of you header concatenated with your claim
jwt="$header.$claims.$(sign $header.$claims)"
ENDPOINT=https://api.sandbox.push.apple.com:443
URLPATH=/3/device/
URL=$ENDPOINT$URLPATH$DEVICETOKEN
curl -v \
--http2 \
--header "apns-push-type: background" \
--header "authorization: bearer $jwt" \
--header "apns-topic: ${BUNDLEID}" \
--data '{"aps":{"content-available" : 1, "data":"Some Data"}}' \
"${URL}"
@drewpitchford
Copy link

@ben-xD the latest version of macOS. I’ll see if there are any updates for curl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment