Search code examples
phpiosapns-php

unable to connect apple server for APNS under distribution


I am using following code in php for APNS notification in distribution. My code works fine for my server but when i shift server to go daddy , app will stop notifications and respond failed to connect : 0 my code is

$message=stripslashes($_POST['message']);
$sql='insert into message set message = "'.$message.'"';
$res=mysql_query($sql);
$query="select deviceId from pushnotification";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
//$deviceToken = '76dcc65ccd155180ff56a15a539173137e38c9dde363d648e5f17343e0940c8d';
$deviceToken = $row['deviceId'];
$passphrase = '******';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err,$errstr, 60,       STREAM_CLIENT_CONNECT, $ctx);
if (!$fp)
 exit("Failed to connect: $err $errstr" . PHP_EOL);
 //echo 'Connected to APNS' . PHP_EOL;
 $body['aps'] = array(
 'alert' => $message,
 'sound' => 'default'
 );
 $payload = json_encode($body);
 $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
 $resultmsg = fwrite($fp, $msg, strlen($msg));
 if (!$resultmsg)
 $msg='Message not delivered' . PHP_EOL;
 else
 $msg='Message successfully delivered' . PHP_EOL;
 fclose($fp);

Any help will be appreciated Thanks


Solution

  • After doing very hotch potch , create my private key again and ssl certificate again for my app Id. And it worked. It was all about app keys and certificates, code is fine.