Apple最近向APNS添加了一种新的身份验证方法(Apple推送通知身份验证密钥(沙盒和生产))。
下载的密钥是.p8
带有私钥的文件:
$ cat APNSAuthKey_3HHEB343FX.p8
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBH...Already.Revoked...lHEjCX1v51W
-----END PRIVATE KEY-----
我正在使用旧方法使用APNs消息-将它们添加到钥匙串中,请求证书并使用OpenSSL将消息发送到gateway.production.push.apple.com:2195
。
如何使用标准格式的标准CLI Linux工具(OpenSSL,Python等)发送推送通知?
如果您的计算机上安装了带有HTTP / 2支持的curl和具有ECDSA支持的openssl,则可以使用以下脚本通过APNs Auth Key测试推送通知:
#!/bin/bash
deviceToken=b27371497b85611baf9052b4ccfb9641ab7fea1d01c91732149c99cc3ed9342f
authKey="./APNSAuthKey_ABC1234DEF.p8"
authKeyId=ABC1234DEF
teamId=TEAM123456
bundleId=com.example.myapp
endpoint=https://api.development.push.apple.com
read -r -d '' payload <<-'EOF'
{
"aps": {
"badge": 2,
"category": "mycategory",
"alert": {
"title": "my title",
"subtitle": "my subtitle",
"body": "my body text message"
}
},
"custom": {
"mykey": "myvalue"
}
}
EOF
# --------------------------------------------------------------------------
base64() {
openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
}
sign() {
printf "$1" | openssl dgst -binary -sha256 -sign "$authKey" | base64
}
time=$(date +%s)
header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$authKeyId" | base64)
claims=$(printf '{ "iss": "%s", "iat": %d }' "$teamId" "$time" | base64)
jwt="$header.$claims.$(sign $header.$claims)"
curl --verbose \
--header "content-type: application/json" \
--header "authorization: bearer $jwt" \
--header "apns-topic: $bundleId" \
--data "$payload" \
$endpoint/3/device/$deviceToken
注意:我使用此脚本的一些细微变化来在带有自制版本的curl和openssl的macOS上进行测试:http : //thrysoee.dk/apns/
@JessThrysoee您的“轻微变化”就像CHAMP一样工作。感谢您发表辛勤工作,所有人都可以看到,正是像您这样的人使社区保持+1
感谢您提供的非常有用的脚本。在我的Mac上,我必须添加“ --with-nghttp2”选项以安装具有http2支持的curl(因为默认情况下,默认情况下,自家自制curl不支持http2):brew install curl --with -nghttp2。(或者,因为我已经安装了curl:brew reinstall curl --with-nghttp2)然后在脚本的curl命令上,我添加了选项:--http2,脚本运行良好。谢谢。
我不断收到“无法加载密钥文件”
请注意,如果使用brew,curl必须使用OpenSSL和nghttp2进行编译:
brew install --with-openssl --with-nghttp2 curl
@Neil我用您的自制curl-openssl更改更新了macOS脚本。谢谢。