I am sending push notification to my ios app using php as my backend. Here is my code:
$deviceToken = "XXXX";
$ctx = stream_context_create();
// ck.pem is your certificate file
stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT'] . '/uploads/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'xxxx');
// 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);
// Create the payload body
$body['aps'] = array(
'alert' => array(
'title' => "haia",
'body' => "how are you",
),
'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));
// Close the connection to the server
fclose($fp);
if (!$result)
return 'Message not delivered' . PHP_EOL;
else
return 'Message successfully delivered' . PHP_EOL;
In the above code i am getting this error
Message: stream_socket_client(): Unable to set private key file `/home/developer/workspace/codeigniter/Ekattor/uploads/ck.pem'
Message: stream_socket_client(): failed to create an SSL handle
Message: stream_socket_client(): Failed to enable crypto
Message: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error)
But when i remove ssl form the stream_socket_client() function it doesn't show any error. If i included ssl in stream_socket_client() it shows the above error. I saw already solved answers but i didn't get any luck. Please someone help me to resolve this