1#!/bin/bash
2
3# To get curl with HTTP/2 and openssl with ECDSA: run 'brew install curl-openssl'
4curl=/usr/local/opt/curl/bin/curl
5openssl=/usr/local/opt/openssl/bin/openssl
6
7--------------------------------------------------------------------------
8
9deviceToken=YOUR_PUSH_TOKEN
10authKey=PATH_TO_YOUR_P8_FILE
11authKeyId=KEY_ID_FOR_YOUR_P8_FILE
12teamId=YOUR_TEAM_ID
13bundleId=YOUR_BUNDLE_ID
14endpoint=https://api.development.push.apple.com
15
16read -r -d '' payload <<-'EOF'
17{
18"aps": {
19 "badge": 1,
20 "alert": {
21 "title": "TEST_PUSH_TITLE",
22 "subtitle": "TEST_PUSH_SUBTITLE",
23 "body": "TEST_PUSH_BODY"
24 },
25 "_sid": "SFMC"
26}
27}
28EOF
29
30# --------------------------------------------------------------------------
31
32base64() {
33$openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
34}
35
36sign() {
37printf "$1"| $openssl dgst -binary -sha256 -sign "$authKey" | base64
38}
39
40time=$(date +%s)
41header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$authKeyId" | base64)
42claims=$(printf '{ "iss": "%s", "iat": %d }' "$teamId" "$time" | base64)
43jwt="$header.$claims.$(sign $header.$claims)"
44
45$curl --verbose \
46--header "content-type: application/json" \
47--header "authorization: bearer $jwt" \
48--header "apns-topic: $bundleId" \
49--data "$payload" \
50$endpoint/3/device/$deviceToken