Apple push notification not working in php

639 views Asked by At

My php script always giving error code 0 for apple push notification. The code which i used is given below

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'passphrase', '');
stream_context_set_option($ctx, 'ssl', 'local_cert', 'TxxxProd.pem'); //TxxxDev.pem
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
stream_set_blocking ($fp, 0); 

$err always returns 0

What we have modified on the server is we have upgraded php version and ssl certificate is renewed.

Current php version is PHP Version 5.6.29

Since the code works before, I can't find it out why it is not working now. As a beginner, I am not aware of the .pem file in the server ?

Do we need to make some modifications on that .pem file?

1

There are 1 answers

1
vineeth On

Try this,

<?php 
$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', 'TxxxProd.pem'); //TxxxDev.pem

$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
stream_set_blocking ($fp, 0); 


$tHost = 'gateway.sandbox.push.apple.com';

$tPort = 2195;

$tToken = '*****************';

$tAlert = 'Hi this is a test message from vineeth';

$tBadge = 8;

$tSound = 'default';

$tPayload = 'APNS Message Handled by LiveCode';

$tBody['aps'] = array (
'alert' => $tAlert,
'badge' => $tBadge,
'sound' => $tSound,
);

$tBody ['payload'] = $tPayload;

$tBody = json_encode ($tBody);

$tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);

if (!$tSocket)

exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);

$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;

// Send the Notification to the Server.

$tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));

if ($tResult)

echo 'Delivered Message to APNS' . PHP_EOL; 

else

echo 'Could not Deliver Message to APNS' . PHP_EOL;

// Close the Connection to the Server.

fclose ($tSocket);

?>