如何使用APN Auth Key和标准CLI工具发送APN推送消息?

Ada*_*tan 38 apple-push-notifications

Apple最近为APNS添加了一种新的身份验证方法(Apple推送通知身份验证密钥(Sandbox&Production)).

在此输入图像描述

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

$ cat APNSAuthKey_3HHEB343FX.p8
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBH...Already.Revoked...lHEjCX1v51W
-----END PRIVATE KEY-----
Run Code Online (Sandbox Code Playgroud)

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

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

Jes*_*oee 58

如果您在计算机上安装了支持HTTP/2支持并且安装了ECDSA支持的openssl,则可以使用以下脚本使用APNs身份验证密钥测试推送通知:

#!/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
Run Code Online (Sandbox Code Playgroud)

注意:我使用此脚本的略微变体来测试使用自制软件 curl和openssl的macOS:http://thrysoee.dk/apns/

  • 需要注意的是,如果使用brew,则必须使用OpenSSL和nghttp2编译curl:brew install --with-openssl --with-nghttp2 curl` (4认同)
  • @JessThrysoee 你的“轻微变化”就像一个 **CHAMP**。感谢您发布您的辛勤工作让所有人看到,正是像您这样的人让社区充满活力 +1 (2认同)

Tee*_*aul 13

您可以使用Apple推送通知身份验证密钥(沙盒和生产)通过NODE JS发送推送通知.Apple已在此链接中提供了一个教程

本教程包含创建Apple推送通知身份验证密钥和设置本地服务器以运行Node JS代码以发送推送通知的所有步骤.您可以在本地计算机中运行代码并测试推送通知.

希望这会有所帮助.


Nic*_*ini 6

Voilà在Php看起来像什么.此脚本返回200 ok状态代码以及生成的令牌ID.

// THE FINAL SCRIPT WITHOUT DEPENDENCIES!!! ...except curl with http2
$device_token = "a0abd886etc...";
//echo $key;
$kid      = "YOURKEYID";
$teamId   = "YOURTEAMID";
$app_bundle_id = "your.app.bundle";
$base_url = "https://api.development.push.apple.com";

$header = ["alg" => "ES256", "kid" => $kid];
$header = base64_encode(json_encode($header));

$claim = ["iss" => $teamId, "iat" => time()];
$claim = base64_encode(json_encode($claim));

$token = $header.".".$claim;
// key in same folder as the script
$filename = "KeyFromApple.p8";
$pkey     = openssl_pkey_get_private("file://{$filename}");
$signature;
openssl_sign($token, $signature, $pkey, 'sha256');
$sign = base64_encode($signature);

$jws = $token.".".$sign;

$message = '{"aps":{"alert":"You are welcome.","sound":"default"}}';

function sendHTTP2Push($curl, $base_url, $app_bundle_id, $message, $device_token, $jws) {

    $url = "{$base_url}/3/device/{$device_token}";
    // headers
    $headers = array(
        "apns-topic: {$app_bundle_id}",
        'Authorization: bearer ' . $jws
    );
    // other curl options
    curl_setopt_array($curl, array(
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
        CURLOPT_URL => $url,
        CURLOPT_PORT => 443,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => $message,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_HEADER => 1
    ));
    // go...
    $result = curl_exec($curl);
    if ($result === FALSE) {
        throw new Exception("Curl failed: " .  curl_error($curl));
    }
    print_r($result."\n");
    // get response
    $status = curl_getinfo($curl);
    return $status;
}
// open connection
$curl = curl_init();
sendHTTP2Push($curl, $base_url, $app_bundle_id, $message, $device_token, $jws);
Run Code Online (Sandbox Code Playgroud)