我正在用REST_controller庫在CodeIgniter中開發Web服務。它只是一個管理請求的控制器,我對每個方法都有一個請求。其中一種方法我試圖用它來發送蘋果通知,所以我需要使用ck.pem文件(它是一個證書)。我知道問題是我沒有.pem文件的好路徑。我應該在codeigniter中放置.pem文件以發送Apple推送通知?
這是我正在使用的代碼。我有控制器文件夾中的ck.pem文件,它不起作用。
function pushtest_get(){
$data = array();
// Put your device token here (without spaces):
$deviceToken = 'be67171fb22f3ea8fa68c13a9f2ba7229caf3a487aa656b20f769fa2b1fa5b7a';
// Put your private key's passphrase here:
$passphrase = 'miguel';
$message = 'Notification de prueba desde servicio web rest';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// 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);
$data['response'] = 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// 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)
$data['response'] = 'Message not delivered' . PHP_EOL;
else
$data['response'] = 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
$response->skills = $data;
$this->response($response, 200);
}
對不起,但這是行不通的。 – croigsalvador