Skip to content

Instantly share code, notes, and snippets.

@ben-xD
Created September 22, 2021 04:10
Show Gist options
  • 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}"
@Chrisursula
Copy link

#hello world

@drewpitchford
Copy link

Getting "command not found" for --http2, --header,and --data. I'm not sure what that's about. It also says "cure: no URL specified".

@ben-xD
Copy link
Author

ben-xD commented Feb 2, 2022

@drewpitchford, perhaps you need to update curl? What system are you using?

@ben-xD
Copy link
Author

ben-xD commented Feb 2, 2022

I recently added other features to this script but put it somewhere else: https://github.com/ben-xD/push/blob/main/tools/ios/send_ios_example You can send background and alert notifications to devices using this.

@drewpitchford
Copy link

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

@ben-xD
Copy link
Author

ben-xD commented Jan 21, 2025

I've created a tool to publish APNs to macOS and iOS (and Android) in https://github.com/ben-xD/push/tree/main/tool

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