1
votes

I am implementing a service for push notification for apple device. using a php script it does push notification only when the php script hit from browser. when i hit the server php script by the browser it pushes the notification to the Apple device. my php script is...

// END OF METHOD TO SEND NOTICATION IN APPLE DEVICES

function sendNotificationToAppleDevice($token,$message)
    {
    /////////////////////////////////////////////For apple divice////////////////////////////////

$passphrase='1234';
// Create a stream to the server
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'sample.pem');
stream_context_set_option($streamContext, 'ssl', 'passphrase', $passphrase);

$apns = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $error,$errorString,60,STREAM_CLIENT_CONNECT, $streamContext);

// You can access the errors using the variables $error and $errorString


// Now we need to create JSON which can be sent to APNS
        $inc=0;                  
$load = array(
'aps' => array(
'alert' => $message,
'badge' =>$inc,
)
);
$inc++;
$payload = json_encode($load);
// The payload needs to be packed before it can be sent
$apnsMessage = chr(0) . chr(0) . chr(32);
$apnsMessage .= pack('H*', str_replace(' ', '', $token));
$apnsMessage .= chr(0) . chr(strlen($payload)) . $payload;

// Write the payload to the APNS

fwrite($apns, $apnsMessage);

echo "just wrote " . $payload;

// Close the connection

fclose($apns);
    }

This script run well and send notification successfully when this script hit by browser.

Now when i run this script from cron job from schedule task it does not send any notification to the device. it caused......

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in sample.php

Warning: stream_socket_client(): Failed to enable crypto in sample.php on line

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push .apple.com:2195 (Unknown error) in sample.php on line

Warning: fwrite() expects parameter 1 to be resource, boolean given in sample.php on line

Warning: fclose() expects parameter 1 to be resource, boolean given in sample.php on line

whats the problem with this function and error..please help me anyone...

Thanks in advance.

1
Are you sure that you provide correct parameters to function? Maybe you should also check permissions for your sample.php - Josip B.

1 Answers

5
votes

After research a lot on it at last i found where is the loop hole..

you have to change just one line to work this method with the cron job..:-)

change this line

stream_context_set_option($streamContext, 'ssl', 'local_cert', 'sample.pem');

to

stream_context_set_option($streamContext, 'ssl', 'local_cert', 'your FULL path of pem file');

for my case i used

stream_context_set_option($streamContext, 'ssl', 'local_cert', 'C:\Websites\games\project_name\sample.pem');

and that's all... hope your problem will be solved now..