Regarding issue in php code for send push notification ios d

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ajaysingh
Forum Newbie
Posts: 2
Joined: Sat Oct 07, 2017 5:23 am

Regarding issue in php code for send push notification ios d

Post by ajaysingh »

I Tried This Code to send notification in ios device.Its Show that 'Message successfully delivered'
but Sometimes its showed msged in device but sometimes not.
don't know why??
where i am wrong??
<?php
//$data_back = json_decode(file_get_contents('php://input'));
$sqltkn="select tokenid from device_ios";

$tknarr = array();
$registrationIds = mysql_query($sqltkn);
while($row = mysql_fetch_array($registrationIds,MYSQL_NUM))

{

array_push($tknarr,$row[0] );

}
//var_dump($tknarr);
//die("test");

// My device token here (without spaces):
$deviceToken = 'd3329f4a7f5237be0dd05904e09654a5d884734ce36449e06b3e708e1e8a9933';

// My private key's passphrase here:
$passphrase = 'xenon';

// My alert message here:
//$message = 'New Push Notification!';
//$message=$data_back->{"message"};
$message=$news_title;
//badge


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev-server.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
stream_set_blocking ($fp, 0);
sleep(1);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);

// Encode the payload as JSON
$payload = json_encode($body);

foreach ($tknarr as $tknid) {



// Build the binary notification
//$msg = chr(0) . pack('n', 32) . pack('H*', $tknid) . pack('n', strlen($payload)) . $payload;
$msg = chr(0) . pack('n', 32) . pack('H*', $tknid) . pack('n', strlen($payload)) . $payload;
//sleep(5);
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
@socket_close($fp);
//fclose($fp);
//socket_close($fp);
fflush($fp);

//usleep(400000);

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
}

// Close the connection to the server
fclose($fp);

/* Output header */

?>
Post Reply