簡體   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