-
Notifications
You must be signed in to change notification settings - Fork 6
/
liveactivity-push-p8.sh
executable file
·67 lines (60 loc) · 1.89 KB
/
liveactivity-push-p8.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh
# P8 file path
P8_FILE="./party/sandbox.p8"
AUTH_KEYID="xxxx"
AUTH_TEAMID="xxxx"
# bundle id
TOPIC="bundle-id"
# token
TOKEN="804f8a2dac2d3a5dd62dae2a93b1eb4847e52e51ffa64cc1839e8e10a4dc54934d9795d6452a7fe1a65934a7d52094d3c46f9988e7cc2bccbac0b9fec60a7639aea8c176530969e5f9ddc2b6f3a320cd"
PUSH_TYPE="liveactivity"
# production environment
# APNS_HOST_NAME=api.push.apple.com
# development environment
APNS_HOST_NAME=api.development.push.apple.com
timestamp=$(date +%s)
read -r -d '' PAYLOAD <<-'EOF'
{
"aps": {
"timestamp": ReplaceWithTimestamp,
"event": "update",
"content-state": {
"friends": [
{
"name": "dawenhing1",
"online": false
},
{
"name": "dawenhing2",
"online": true
}
]
},
"alert": {
"title": "Delivery Update",
"body": "Your pizza order will arrive soon.",
"sound": "push.caf"
}
}
}
EOF
PAYLOAD=${PAYLOAD//ReplaceWithTimestamp/${timestamp}}
python3 -c "import sys,json;p=r'''${PAYLOAD}''';print(p);json.loads(p);"
# --------------------------------------------------------------------------
base64() {
openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
}
sign() {
printf "$1"| openssl dgst -binary -sha256 -sign "$P8_FILE" | base64
}
TIMESTAMP=$(date +%s)
HEADER=$(printf '{ "alg": "ES256", "kid": "%s" }' "$AUTH_KEYID" | base64)
CLAIMS=$(printf '{ "iss": "%s", "iat": %d }' "$AUTH_TEAMID" "$TIMESTAMP" | base64)
JWT="$HEADER.$CLAIMS.$(sign $HEADER.$CLAIMS)"
curl \
--header "content-type: application/json" \
--header "authorization: bearer $JWT" \
--header "apns-topic: $TOPIC.push-type.liveactivity" \
--header "apns-push-type: ${PUSH_TYPE}" \
--data "$PAYLOAD" \
--http2 https://$APNS_HOST_NAME/3/device/$TOKEN