I have got some problem and tried all suggestion but no help. I am not getting what is the error. Everything is to be going perfect but when call fwrite() then it will give error and stop further execution.
If anyone has an idea about it, please share with us. Thanks.
Here is my function that i am using for send push notification to iOS using PHP laravel:
function send_custom_IOSNotification($registatoin_ids, $finalArrayVal, $title, $env = false) {
$passphrase = '1234';
$ctx = stream_context_create();
$path = storage_path();
if($env) {
stream_context_set_option($ctx, 'ssl', 'local_cert', $path.'/apns-dev-cert.pem');
} else {
stream_context_set_option($ctx, 'ssl', 'local_cert', $path.'/pushcert.pem');
}
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'cafile', $path.'/entrust_2048_ca.cer');
if($env) {
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
} else {
$fp = stream_socket_client('tls://gateway.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
}
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
$arrlength=count($registatoin_ids);
Log::useFiles(storage_path().'/logs/notification.log');
for($x=0;$x<$arrlength;$x++)
{
$finalArrayVal['aps'] = array(
'alert' => $title,
'badge' => $registatoin_ids[$x]['badge'],
'sound' => 'default'
);
foreach($finalArrayVal as $key => $value){
$finalArrayVal[$key] = array_map('strval', $finalArrayVal[$key]);
}
updateUserBadges($registatoin_ids[$x]['user_id'],$registatoin_ids[$x]['badge']);
$payload = json_encode($finalArrayVal);
$msg = chr(0) . pack('n', 32) . pack('H*', $registatoin_ids[$x]['device_id']) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) {
Log::info('Message not delivered at '.$x);
fclose($fp);
if($env) {
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
} else {
$fp = stream_socket_client('tls://gateway.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
}
} else {
Log::info('Message successfully delivered at '.$x);
}
}
fclose($fp) }