用于推送通知的iOS 13 PHP脚本有效负载 [英] iOS 13 PHP Script payload for Push notification

查看:166
本文介绍了用于推送通知的iOS 13 PHP脚本有效负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是知道iOS 13已更改了Push通知设备令牌和有效负载. 我有更新的应用程序并发布了它.现在,我们在Php中拥有服务器,我们向用户发送警报和静默通知. 我已经经历过"/sending_notification_requests_to_apns 苹果文档,但我仍然无法理解如何在有效负载中传递新标头.

I just come to know that iOS 13 have changed Push notification device token and Payload. I have update App and published it. Now, We have server in Php, We send both alert and silent notification to the users. I have gone through "https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns" Apple Documentation, but I am still not able to understand how can I pass new headers in payload.

这是我的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 Push通知创建有效负载.任何帮助将不胜感激.

Can anyone have any example, tutorial or PHP Script so I can create payload for Apple Push notification. Any help will be appreciated.

推荐答案

我会尽力帮助您-

这将仅作为测试"工作-不是生产性的,但它会向正确的方向发送信息-

This will just work as a 'test' - it is not production but it will send you in the right direction -

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

So - A long long time ago Apple changed their push notifications to return information about the push and also get rid of their feedback server. That is part of the change you are having to work with.

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

Here is a basic PHP setup - for using the NEW PUSH AUTH -

您需要做的就是为您的开发人员帐户获取一个按键-您为所有帐户获得一个.您可以通过在Apple开发人员页面上注册其他证书的方式获得此信息-

What you will need to do is get a push key for your developer account - YOU GET ONE FOR ALL YOUR ACCOUNTS. You get this the same way you register for your other certs on the apple developer page -

以下是一些示例PHP代码,可以帮助您入门-此要求在PHP 5.3以上(我不知道确切的版本)

Here is some sample PHP code to get you started - THIS REQUIRES ABOVE PHP 5.3 (I dont know the exact version)

<?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

这是一个测试"设置-请不要将其用于生产环境,因为如果您需要进行大量请求,则希望重用该连接.

This is a 'test' setup - please do not use this for production because if you need to make A LOT of requests you want to reuse the connection.

这篇关于用于推送通知的iOS 13 PHP脚本有效负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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