【问题标题】:iOS 13 PHP Script payload for Push notification用于推送通知的 iOS 13 PHP 脚本负载
【发布时间】:2020-02-20 21:30:38
【问题描述】:

我才知道 iOS 13 更改了推送通知设备令牌和有效负载。 我已经更新了 App 并发布了它。现在,我们在 PHP 中有服务器,我们向用户发送警报和静默通知。 我已经浏览了“https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns”Apple 文档,但我仍然无法理解如何在有效负载中传递新的标头。

这是我的 PHP 脚本。

<?php

// Put your device token here (without spaces):
$deviceToken = 'deviceToken';

// Put your private key's passphrase here:
$passphrase = 'dummyPassword';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
//stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');

// Open a connection to the APNS server
$fp = stream_socket_client(
  'ssl://gateway.sandbox.push.apple.com:2195', $err,
  $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
  exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

    $body['aps'] = array(
                         'content-available' => '1',
                         'type' => 'message',
                         'msg' => 'We detected a failure when sending a message.'
                             );

    // Encode the payload as JSON
    $payload = json_encode($body);


    echo "\njson::::\n";
    print_r ($payload);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
  echo 'Message not delivered' . PHP_EOL;
else
  echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

任何人都可以提供任何示例、教程或 PHP 脚本,以便我可以为 Apple 推送通知创建有效负载。任何帮助将不胜感激。

【问题讨论】:

标签: php ios apple-push-notifications ios13 ios13.1.2


【解决方案1】:

我会尽力帮助你的——

这只是一个“测试”——它不是生产,但它会引导你朝着正确的方向前进——

所以—— 很久以前,Apple 更改了他们的推送通知以返回有关推送的信息,并摆脱了他们的反馈服务器。这是您必须进行的更改的一部分。

这是一个基本的 PHP 设置 - 用于使用 NEW PUSH AUTH -

您需要做的是为您的开发者帐户获取一个按键 - 您的所有帐户都可以获得一个按键。您获得此证书的方式与在 Apple 开发者页面上注册其他证书的方式相同 -

这里有一些示例 PHP 代码可以帮助您入门 - 这需要 PHP 5.3 以上(我不知道确切的版本)

<?php
const AUTH_KEY_PATH = '/path/to/AuthKey.p8';
const AUTH_KEY_ID = 'your auth key id here';
const TEAM_ID = 'your team id here';
const BUNDLE_ID = 'com.testApplication.me';
// Setup the payload
$payload = [
   'aps' => [
     'alert' => [
       'title' => 'This is the notification.',
     ],
     'sound'=> 'default',
   ],
];
//// Create The JWT
$header = base64_encode(json_encode([
             'alg' => 'ES256',
             'kid' => AUTH_KEY_ID
        ]));
$claims = base64_encode(json_encode([
             'iss' => TEAM_ID,
             'iat' => time()
        ]));
$pkey = openssl_pkey_get_private('file://' . AUTH_KEY_PATH);
openssl_sign("$header.$claims", $signature, $pkey, 'sha256');
$signed = base64_encode($signature);
$signedHeaderData = "$header.$claims.$signed";
//Setup curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'apns-topic: ' . BUNDLE_ID,
  'authorization: bearer ' . $signedHeaderData,
  'apns-push-type: alert'
]);
//Setting up URL
$token = $argv[1];
$url = "https://api.development.push.apple.com/3/device/$token";
//Making the call
curl_setopt($ch, CURLOPT_URL, "{$url}");
$response = curl_exec($ch);
// DEAL WITH IT ('it' being errors)
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

要使用它,你会

php -f push.php TOKEN

这是一个“测试”设置 - 请不要将其用于生产,因为如果您需要发出大量请求,您想重用连接。

【讨论】:

  • 我们如何发送设备 ID 数组?
猜你喜欢
  • 2017-07-20
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多