php - 用于推送通知的 iOS 13 PHP 脚本负载

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

我刚刚知道 iOS 13 更改了推送通知设备 token 和有效负载。 我已经更新了应用程序并发布了它。现在,我们在 Php 中有服务器,我们向用户发送警报和静默通知。 我已经阅读了“https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns”Apple文档,但我仍然无法理解如何在有效负载中传递新 header 。

这是我的 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 设置 - 用于使用 NEW PUSH AUTH -

您需要做的是为您的开发者帐户获取一个推送 key - 您的所有帐户都拥有一个。您获得此证书的方式与在苹果开发者页面上注册其他证书的方式相同 -

这里有一些示例 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

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

关于php - 用于推送通知的 iOS 13 PHP 脚本负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58543104/

相关文章:

php - 回显查询的值

php - 将 BBCode [IMG] 转换为 <img>

ios - xmpp - 在 ios 中使用 turnsocket 发送/接收文件

ios - 使用 localhost 使用 iOS 9.2 和 Xcode 7.2 访问本地服务器

iphone - 只允许用户按 UIButton 一次

java - 将 Apple APNs 身份验证 key 加载到 Java PrivateKey

PHP 类常量——公共(public)的、私有(private)的还是 protected ?

php - 更新/覆盖 amazon s3 中文件(对象)的 acl

objective-c - Web服务应用程序的设计模式(使用APNS)

iphone - 私有(private) APNS 服务器?