We have developed an iOS App on Ionic Framework. The server side code is in PHP. We are not able to send push notification through PHP API to APNS server ( on iPhone device ).
Analysis :
We have investigated our side that required ports (2195 and 2196) are open, firewall is enabled at hosting site.
We have generated .pem file and Device token on client side and tested successfully using java api but facing issue with php api. We received response by using PHP code - Connection timeout
We need to solve this issue ASAP.
PHP Code :
$deviceToken ='ji78c2433a76b2d977a4abe0f01306dd956ac4c4a3679a70b95d';
$passphrase = '**********';
$message = 'hi ...its a test push';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '[login to view URL]');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://[login to view URL]', $error,
$errorString, 60,
STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
{
echo 'Iphone Notification : Failed to connect:-'.$errorString;
// echo 'not Connected to APNS'.PHP_EOL;
}
else
{
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
"badge"=> 1
);
// 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)
echo 'Iphone Notification : Message not delivered';
else
echo 'Iphone Notification : Message delivered';
// Close the connection to the server
fclose($fp);
}
I have 20+ years of experience working on Linux, Networking, shell scripting, various programming languages and related application frameworks. I have lead team who have developed mobile apps on Android and iOS platform for my corporate clients. I have been instrumental in publishing couple of those apps on Apple App Store, thoroughly aware of pain points in publishing the applications.
I have long track record of working on, obvious but difficult to diagnose problems.
The code you have posted is pretty boiler plate code from the online blogs. Few issues I can immediately think of to diagnose this issue:
1. File permissions
2. Host based firewall (on server itself), or actually port is not open for server you are trying to use
3. Time out setting due to slow connection, DNS not getting resolved, recipient IP is blocked
4. Certificate is expired (for some reason)
and may be few other things if above cannot resolve problem.
I will need access to your server (direct SSH preferred) or through reliable remote access to solve this problem or else may need sample code, certificate and passphrase. It cannot be solved in isolation remotely.
I will also request to get continuous access to your server till problem is resolved because to resolve problems like this, one need to try various stuffs, in sequence. I will prefer to work overnight and close this issue at the earliest.
Request further discussion if you want any clarifications or have concerns about this proposal