【问题标题】:Apple push notification with cURL带有 cURL 的 Apple 推送通知
【发布时间】:2013-01-07 17:15:25
【问题描述】:

有没有办法通过使用 cURL(而不是 stream_socket_client)在 PHP 中实现一个发布推送通知系统?

我写了:

$url = 'https://gateway.sandbox.push.apple.com:2195';
$cert = 'AppCert.pem';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "passphrase");

$curl_scraped_page = curl_exec($ch);

但是如何设置设备令牌和json消息呢?

【问题讨论】:

  • 为什么不看看现成的库> easyapns.com
  • @BenM 因为我不想使用 stream_socket_client 函数,因为它不允许使用 ssl 协议的代理选项

标签: php ios curl notifications push


【解决方案1】:

这是设备令牌和json消息的代码,我想这会对你有所帮助。

$url = 'https://gateway.sandbox.push.apple.com:2195';
$cert = 'AppCert.pem';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "passphrase");
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"device_tokens": ["458e5939b2xxxxxxxxxxx3"], "aps": {"alert": "test message one!"}}');

$curl_scraped_page = curl_exec($ch);

【讨论】:

  • 不起作用,根据文档,您需要二进制格式而不是完整的 json,因此它不应该起作用。它看起来像 Urban Airship 推送端点的 API。
  • 不工作。而且您不能通过 HTTP 向 Apple 发送推送通知。您确实必须使用 TCP 二进制通信。
【解决方案2】:

此代码有效

$deviceToken = '...';
$passphrase = '...';
$message = '...';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxx.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$body['aps'] = array('alert' => $message,'sound' => 'default');
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
fclose($fp);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-11-18
  • 2016-08-06
  • 2011-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
相关资源
最近更新 更多