I have got same questions in stackoverflow and tried all answers but no help. I am not getting what is the error.
Here is my function that i am using:
function sendRegistryNotification($message, $deviceToken, $deviceType, $batchcount)
{
$message=$message;
$batchcount=$batchcount;
$path=ABSPATH.'api'.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'Certificates2.pem';
$deviceType = strtolower($deviceType);
switch ($deviceType) {
case 'ios':
$pemPath = $path; // replace with the path to PEM file
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $pemPath);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',
$err,
$errstr,
60,
STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,
$ctx);
// Create the payload body
$body['aps'] = array(
'badge' => +$batchcount,
'alert' => $message,
'sound' => 'default'
);
$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));
var_dump($result);
if (!$result)
{
fclose($fp);
return false;
}
else
{
fclose($fp);
return true;
}
break;
case 'android':
$message = array("message" => $message,'title'=> 'eyLog','msgcnt'=>$batchcount);
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => array($deviceToken),
'data' => $message,
);
$headers = array(
'Authorization: key='.'AIzaSyDud68-R31c9SPq4dXZblt0JBPi4TBaneQ', // Key For eylog App
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
//var_dump($result);
if ($result === FALSE) {
// die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
return TRUE;
break;
}
return true;
}
Here is the error that i am getting:
{
"status": "failure",
"uniquediary_id": "14407503971",
"message": "fwrite(): SSL operation failed with code 1. OpenSSL Error messages:\nerror:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry"
}
I am not getting why it is happening. and the code only gives error in server in my localhost it is working fine