PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 16, 2017 11:12 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Sat Oct 07, 2017 5:37 am 
Offline
Forum Newbie

Joined: Sat Oct 07, 2017 5:23 am
Posts: 2
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 */

?>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group