如何使用 APNs Auth Key 和标准 CLI 工具发送 APNs 推送消息? [英] How to send APNs push messages using APNs Auth Key and standard CLI tools?

查看:29
本文介绍了如何使用 APNs Auth Key 和标准 CLI 工具发送 APNs 推送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple 最近为 APNS 添加了一种新的身份验证方法(

下载的密钥是一个带有私钥的.p8文件:

$ cat APNSAuthKey_3HHEB343FX.p8-----开始私钥-----MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBH...Already.Revoked...lHEjCX1v51W-----结束私钥-----

我正在使用旧方法使用 APNs 消息 - 将它们添加到钥匙串,请求证书并使用 OpenSSL 将消息发送到 gateway.production.push.apple.com:2195.>

如何使用标准 CLI Linux 工具(OpenSSL、Python 等)使用新格式发送推送通知?

解决方案

如果您的机器上安装了支持 HTTP/2 的 curl 和支持 ECDSA 的 openssl,您可以使用以下脚本来测试使用 APNs 身份验证的推送通知关键:

#!/bin/bashdeviceToken=b27371497b85611baf9052b4ccfb9641ab7fea1d01c91732149c99cc3ed9342fauthKey=./APNSAuthKey_ABC1234DEF.p8"authKeyId=ABC1234DEFteamId=TEAM123456bundleId=com.example.myapp端点=https://api.development.push.apple.com读取 -r -d '' 有效载荷 <<-'EOF'{aps":{徽章":2,类别":我的类别",警报":{标题":我的标题",副标题":我的副标题",正文":我的正文信息"}},自定义":{mykey":myvalue"}}EOF# --------------------------------------------------------------------------base64(){openssl base64 -e -A |tr -- '+/' '-_' |tr -d =}标志() {printf "$1";|openssl dgst -binary -sha256 -sign "$authKey";|base64}时间=$(日期+%s)header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$authKeyId" | base64)claim=$(printf '{ "iss": "%s", "iat": %d }' "$teamId" "$time" | base64)jwt=$header.$claims.$(sign $header.$claims)";curl --verbose \--header "content-type: application/json";\--header "授权: 不记名 $jwt";\--header "apns-topic: $bundleId";\--data "$payload";\$endpoint/3/device/$deviceToken

注意:我使用此脚本的一个细微变化在 ma​​cOS with homebrew 版本的 curl 和 openssl 上进行测试:http://thrysoee.dk/apns/

Apple 现在有关于这个方法的文档,用于使用令牌发送推送通知.

Apple recently added a new authentication method to APNS ( Apple Push Notification Authentication Key (Sandbox & Production)).

The downloaded key is a .p8 file with a private key:

$ cat APNSAuthKey_3HHEB343FX.p8
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBH...Already.Revoked...lHEjCX1v51W
-----END PRIVATE KEY-----

I am using APNs messages using the old method - adding them to keychain, asking for a certificate and using OpenSSL to send messages to gateway.production.push.apple.com:2195.

How do I send push notifications using standard CLI Linux tools (OpenSSL, Python etc.) using the new format?

解决方案

If you have curl with HTTP/2 support and openssl with ECDSA support installed on your machine, you can use the following script to test push notifications using an 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

NOTE: I use a slight variation of this script for testing on macOS with homebrew versions of curl and openssl: http://thrysoee.dk/apns/

Apple now have documentation for this method to Send a Push Notification Using a Token.

这篇关于如何使用 APNs Auth Key 和标准 CLI 工具发送 APNs 推送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆