php - Push notification is not working sometime and sometime it works. getting warning SSL3_WRITE_PENDING:bad write retry and SSL: Broken pipe -
$passphrase = ''; $devicetoken="8ebf42fd63c37071b9309f938a527189525b777f35571fc249ec1ed2581d3441"; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', "pushcert.pem"); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // open connection apns server $fp = stream_socket_client( 'ssl://gateway.push.apple.com:2195', $err, $errstr, 60, stream_client_connect|stream_client_persistent, $ctx); if (!$fp) { exit("failed connect: $err $errstr" . php_eol); return 0; } // create payload body $body['aps'] = array( 'notification_type_id'=>1, 'contest_id'=>1, 'pic_id'=>1, 'rid'=>1, 'alert' => "hi john test push", 'sound' => 'default' ); $payload = json_encode($body); $msg = chr(0) . pack('n', 32) . pack('h*', $devicetoken) . pack('n', strlen($payload)) . $payload; $result = fwrite($fp, $msg, strlen($msg)); if (!$result) { echo 'message not delivered' . php_eol; }else{ echo 'message delivered' . php_eol; }
why push notification work sometime , sometime not? getting below warnings when sending push notification ios device
severity: warning
message: fwrite(): ssl operation failed code 1. openssl error messages: error:1409f07f:ssl routines:ssl3_write_pending:bad write retry
filename: models/cron_win_contest_model.php
line number: 386
or
severity: warning
message: fwrite(): ssl: broken pipe
filename: models/cron_win_contest_model.php
line number: 386
could please me fix issue
when trying write socket. if goes wrong, fwrite return false
or 0
(depending on php version) return value. when happens, must manage it.
by way, add sleep(5) before fwrite().
you can :-
try { $result = fwrite($fp, $msg, strlen($msg)); } catch (exception $ex) { sleep(5); //sleep 5 seconds $result = fwrite($fp, $msg, strlen($msg)); if ($result) { return true; } else { return false; } }
hope helps!
Comments
Post a Comment