Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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}"
@Chrisursula

This comment has been minimized.

Copy link

@Chrisursula Chrisursula commented Oct 8, 2021

#hello world

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