繁体   English   中英

如何使用TLS和PHP发送iOS推送通知?

[英]How to send iOS Push Notifications using TLS and PHP?

我的应用程序仍在开发中,我使用本教程使用PHP和SSL发送iOS推送通知。

http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

它正在运行但最近被折旧,因为Apple最近决定放弃SSL立即影响开发中的所有应用程序,生产中的应用程序将在10月29日之前更改其代码。

我想知道如何使用TLS而不是SSL来做同样的事情。

这是我以前工作的php看起来像:

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.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);

我尝试添加一个Entrust证书,正如Apple建议的那样:

$ctx = stream_context_create();
stream_context_set_option($ctx, 'tls', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'tls', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'tls', 'cafile', 'entrust_2048_ca.cer');
$fp = stream_socket_client('tls://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

但它仍然无效。 你有什么建议来解决它吗?

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');
$fp = stream_socket_client('tls://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

如果您在localhost环境中工作,请不要忘记下载认证文件entrust_2048_ca.cer

<?php
$message = 'aa_' . rand(10000,99999);

$deviceToken = array(
    'xxxxxx'
);

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'passphrase', '111111');
stream_context_set_option($ctx, "ssl", "local_cert", './apns.pem');

$fp = NULL;
$errno = NULL;
$errstr = NULL;

$fp = stream_socket_client("tls://gateway.sandbox.push.apple.com:2195", $errno, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if($fp === FALSE){
    exit('error message');
}

$content = array("aps" => array("alert" => $message, "badge" => 4, "sound" => 'default', "code" => 200));
$data = json_encode($content);

foreach ($deviceToken as $token) {
    $msg = chr(0) . pack("n", 32) . pack("H*", $token) . pack("n", strlen($data)) . $data;
    fwrite($fp, $msg);
    fflush($fp);
}

fclose($fp);

以下是一些可以帮助您弄清楚的提示:

  1. 转到entrust.net/downloads/root_request.cfm并下载entrust_2048_ca.cer

  2. 添加以下代码:stream_context_set_option($ ctx,'ssl','cafile','entrust_2048_ca.cer');

  3. 确保路径是否正确:'../folder/file/ck.pem'?

  4. 切换并尝试沙箱和实时ssl链接。

  5. 切换开发和生产pem并尝试两者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM