Not sure if it's the same issue as iPhone SDK Push notification randomly fails . But some notifications do arrive, I do have does "unpack" warnings when upacking command 8 packets.
I have a php file that pulls any notifications from a table (DB) which has token, payload, and device (iphone,ipad). This php runs every 15 seconds on the server cron. I don't know why 20% of the notifications arrive sometimes.... sometimes 80% arrive.... and sometime none arrive (I have 10 devices for testing). I read that apples notifications are "best effort" and that there are not guaranteed to arrive but this is ridiculous, something must be wrong with the logic of my code. Here is my code, I have changed names of directories, DB info and files for privacy reasons. Please help.
<?php
include("/#####/DB.conf");
$link = mysql_connect($host,$userDB,$passDB) or die("Failed to connect: " . mysql_error());
mysql_select_db('#######') or die("Failed to connect: " . mysql_error());
mysql_query("SET NAMES 'utf8'");
$certFile_iphone = '/##########/push_production.pem';
$certFile_ipad = '/########/push_production.pem';
$passphrase = '#########';
$sent += sendPush($certFile_iphone,$passphrase,'iphone');
$borrados += checkFeedback($certFile_iphone,$passphrase,'iphone');
$sent += sendPush($certFile_ipad,$passphrase,'ipad');
$borrados += checkFeedback($certFile_ipad,$passphrase,'ipad');
//////////////////////////////
//Send notification to users//
//////////////////////////////
function sendPush($cert,$pass,$device)
{
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 600, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
//gets all the devices of that type and their current notification to send
$query = mysql_query("SELECT * FROM pushNotification_buffer WHERE device = '".$device."' LIMIT 1000;");
$errors = 0;
$sent = 0;
$i = 0;
while($row = mysql_fetch_array($query))
{
$payload = $row['payload'];
$deviceToken = $row['identificador'];
//$msg = chr(0) . pack('n', 32) . pack('H*', $row['identificador']) . pack('n', strlen($payload)) . $payload;
$msgInner =
chr(1)
. pack('n', 32)
. pack('H*', $deviceToken)
. chr(2)
. pack('n', strlen($payload))
. $payload
. chr(3)
. pack('n', 4)
. chr($i)
. chr(4)
. pack('n', 4)
. pack('N', time() + 86400)
. chr(5)
. pack('n', 1)
. chr(10)
;
$msg=
chr(2)
. pack('N', strlen($msgInner))
. $msgInner
;
$i++;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
{
$errors++;
usleep(1000000);
$result = fwrite($fp, $msg, strlen($msg));
error_log("Error on Device (2): " . $row['identificador']);
}
if (!$result)
{
$errors++;
fclose($fp);
return $sent;
}
else
{
$sent++;
mysql_query("DELETE FROM pushNotification_buffer WHERE identificador = '".$row['identificador']."' AND payload = '".mysql_real_escape_string($row['payload'])."' LIMIT 1;");
}
error_response($fp);
}
fclose($fp);
return $sent;
}
/////////////////
//APNS FeedBack//
/////////////////
function checkFeedback($cert,$pass,$device){
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://feedback.push.apple.com:2196', $error, $errorString, 600, STREAM_CLIENT_CONNECT, $ctx);
$toDelete = 0;
if(!$fp) echo ("NOTICE: Failed to connect to device: $error - $errorString.");
while ($devcon = fread($fp, 38)){
$arr = unpack("H*", $devcon);
$rawhex = trim(implode("", $arr));
$token = substr($rawhex, 12, 64);
if(!empty($token))
{
if($device == 'iphone'){
mysql_query("DELETE FROM iphoneDevicesAPNS WHERE identificador = '" . $token . "' LIMIT 1;");
}else{
mysql_query("DELETE FROM ipadDevicesAPNS WHERE identificador = '" . $token . "' LIMIT 1;");
}
$toDelete++;
}
}
fclose($fp);
return $toDelete;
}
/////////////////////////////
//Error response, command 8//
/////////////////////////////
function error_response($fp){
$read = array($fp);
$null = null;
$changedStreams = stream_select($read, $null, $null, 0, 1000000);
if ($changedStreams === false)
{
echo ("Error: Unabled to wait for a stream availability");
}
elseif ($changedStreams > 0){
$responseBinary = fread($fp, 6);
if ($responseBinary !== false || strlen($responseBinary) == 6){
$response = unpack('Ccommand/Cstatus_code/Nidentifier', $responseBinary);
var_dump($response);
if (strlen($response["command"] > 0) || strlen($response["status_code"] > 0) || strlen($response["identifier"] > 0)){
$texto=
"------------------------------------------". "\n".date("Y-m-d G:i:s"). "\n";
"id: " . $response["identifier"] . "\n" .
"code: ". $response["status_code"] . "\n" .
"command: ". $response["command"] . "\n". "\n"
;
file_put_contents ( "/#####/logAPNS.log" ,$texto, FILE_APPEND);
}
}
}
}
?>
I do have does "unpack" warnings when upacking command 8 packets
This implies that you are getting error responses from Apple, but from what I see in the code, you don't do anything with these errors beside logging them. Each such error response means that you sent invalid data to Apple, and as a result Apple closed the connection. This means any message sent after the invalid one will be discarded until you open a new connection.
The proper way to handle such responses is to parse the bad message ID contained in them, open a new connection to APNS, and re-send all the messages that you sent after the bad message.
The most likely error is invalid device token. That usually happen when you send a development token to production push environment or vice versa. If you clear the invalid device tokens from your DB, you are likely to see 100% of the messages arriving.
Related
I'm having some problems with my implementation because it crash in a random way after 400 - 500 sendings.
I was wondering why the best APNS tutorials for PHP and other platforms advice you to sleep a little (o a lot!) after sending some messages to the APNS server.
Why to sleep your code when sending iOS Push Notifications?
The crash I get is
<html><head><title>500 Internal Server Error</title></head><body>
<h1>Internal Server Error</h1>
<p><i>stream_socket_client(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known</i></p>
<p>#0 /Users/MyUser/development/projects/project/project-api/helpers/PushNotificationHelper.php:407 stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',0,'',60,5,NULL)<br />...
My source code:
static function pushIOS($ios_devices, $push_type, $data_array){
$log = new Logger('PushConsumer');
$log->pushHandler(new StreamHandler(BASEPATH.'log/push.log', Logger::DEBUG));
// set time limit to zero in order to avoid timeout
set_time_limit(0);
// this is the pass phrase you defined when creating the key
$passphrase = 'mypass';
// load your device ids to an array
$deviceIds = $ios_devices;
// this is where you can customize your notification
//payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';
$msg = array
(
'type' => $push_type,
'message' => $data_array,
);
$payload = json_encode($msg);
$result = 'Start' . '<br />';
////////////////////////////////////////////////////////////////////////////////
// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', BASEPATH.'scripts/certificates/appleck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$log->addDebug(count($deviceIds)." devices will receive notifications.'");
//echo count($deviceIds) . ' devices will receive notifications.<br />';
// Open a connection to the APNS server
$fp = stream_socket_client(F3::get('apns_url'), $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) {
$log->addDebug("Failed to connect: $err $errstr");
exit("Failed to connect: $err $errstr" . '<br />');
} else {
$log->addDebug("Apple service is online.");
foreach ($deviceIds as $item) {
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) {
$log->addDebug("Undelivered message to push token: $item");
//echo 'Undelivered message count: ' . $item . '<br />';
} else {
$log->addDebug("Delivered message to push token: $item");
//echo 'Delivered message count: ' . $item . '<br />';
}
}
fclose($fp);
$log->addDebug("The connection has been closed by the client");
}
$log->addDebug(count($deviceIds)." devices have received notifications");
// wait for some time
sleep(2);
}
The line with the crash:
$fp = stream_socket_client(F3::get('apns_url'), $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
I have set up a php file on my own server and used it to send push notifications. I have already tried it with two devices and it works well. The problem is with the feedback method which returns nothing. I have already tried everything and disabled firewall and so on. Note that i tested the feedback like this: I run the app on both devices. They both get the notification. then i delete the app on one of them and try again. Here is the PHP code i used:
<?php
$deviceToken1 = '.....';
$deviceToken2 = '.....';
$tokenArr = array($deviceToken1,$deviceToken2);
$passphrase = '.....';
$message = $_POST['message'];
$newsID = $_POST['newsID'];
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
for($i = 0; $i < count($tokenArr); $i++)
{
$body['aps'] = array(
'alert' => $message,
'badge' => 1,
'sound' => 'sound.aiff',
'newsID' => $newsID);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $tokenArr[$i]) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) {
echo '<br/>'.date("Y-m-d H:i:s").' Message not delivered' . PHP_EOL;
}
else {
echo '<br/>'.date("Y-m-d H:i:s").' Message successfully delivered' . PHP_EOL;
}
}
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
$apns = stream_socket_client('ssl://feedback.sandbox.push.apple.com:2196', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
echo $apns;
if(!$apns)
{
echo '<br/>';
echo "Error $errcode : $errstr\n";
return;
}
else {
echo "APNS feedback results\n";
while ($devcon = fread($fp, 38))
{
$arr = unpack("H*", $devcon);
$rawhex = trim(implode("", $arr));
$feedbackTime = hexdec(substr($rawhex, 0, 8));
$feedbackDate = date('Y-m-d H:i', $feedbackTime);
$feedbackLen = hexdec(substr($rawhex, 8, 4));
$feedbackDeviceToken = substr($rawhex, 12, 64);
echo "TIMESTAMP:" . $feedbackDate . "\n";
echo "DEVICE ID:" . $feedbackDeviceToken. "\n\n";
}
}
?>
I have submit my app to apple store and now I want to send APNS to the users .
I want to send 7000 push messages through this php code from my desktop:
<?php
// Put your private key's passphrase here:
$passphrase = '*****';
// Put your alert message here:
$message = 'text here';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.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);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS</br>' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
////////////////////////////////////////////////////////////////////////
function selectfromdb(){
$x = array();
$con = mysql_connect("localhost","root","root");
mysql_select_db("my_db", $con);
mysql_query("set character_set_server='utf8'");
mysql_query("set names 'utf8'");
$result = mysql_query("SELECT idip FROM id");
$c = 0;
while($r = mysql_fetch_array($result))
{
$x[$c] = $r['idip'];
$c++;
}
return $x;
}
///////////////////////////////////////////////////////////////////////
$y = selectfromdb();
$i = 0;
while ($i < sizeof($y)){
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*',$y[$i]) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered</br>' . PHP_EOL;
else
echo 'Message successfully delivered</br>' . PHP_EOL;
$i++;
}
echo 'end</br>';
// Close the connection to the server
fclose($fp);
?>
but it just sent for 507 , then it says : Message not delivered
for all the rest .
The problem is that apple closes the connection after X messages that were sent to unavailable device-IDs.
Clean up your collection / database of deviceids by consuming the APNS feedback service, then finish the deployment of your APNS messages.
i have a little problem with my PHP Code.
I did not get it to work, that the message i sent, will receive on all Devices witch are saved in my Database.
The Push App on the Device registers automatically the deviceToken in my MySQL Database. That is all working fine.
The big problem is, that i get the Push Message only to the FIRST Device from the Database. The other Device don't receive any message.
Here my code:
<?php
require_once('konfiguration.php');
$sql = "SELECT deviceToken FROM DeviceTokens";
$dbquery = mysql_query($sql);
// check checkboxes
$message = $_POST['alert'];
$badge = $_POST['badge'];
$sound = $_POST['sound'];
// Construct the notification payload
$body = array();
// anti-error variable
$nothing = true;
// get user input
if ($message) { $alert=$_POST['message']; $nothing = false;
$body['aps']['alert'] = $alert; }
if ($badge) { $nothing = false;
if($_POST['number']) $number=(int)$_POST['number'];
else $number=1;
$body['aps']['badge'] = $number; }
if ($sound) { $nothing = false;
$body['aps']['sound'] = 'beep.wav'; }
// if input not correct, scream
if ($nothing || ($message && $alert=="")) { echo "Falscher Input!"; exit(); }
//make new stream context
$ctx = stream_context_create();
// set parameters
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'w3workProd.pem');
//stream_context_set_option($streamContext, 'ssl', 'passphrase', 'nils');
$socketClient = stream_socket_client('ssl://gateway.push.apple.com:2195', $error,
$errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$payload = json_encode($body);
print "sending message :" . $payload . "\n";
while($row = mysql_fetch_array($dbquery)) {
$message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken))
.chr(0) . chr(strlen($payload)) . $payload;
$deviceToken = str_replace(' ', '', $row['deviceToken']);
$message = pack('CnH*', 0, 32, $deviceToken);
$message = $message . pack('n', strlen($payload));
$message = $message . $payload;
fwrite($socketClient, $message);
echo ($row['deviceToken']);
}
fclose($socketClient);
exit();
?>
Would be nice if you can help.
Why is it working with one Device...and not with the others...
have you tried
STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT
?
I use php to send push message to apns , i use "Enhanced notification format" to sent .. but i can not get the return" Codes in error-response packet" anyone can help me ?? here is my code
<?php
header("Content-Type: text/html; charset=UTF-8");
$deviceToken = "123";
$content = "testing";
if(isset($content))
{
$newContent=substr($content,0,30)."...";
$re_content=iconv("GB2312","UTF-8",$newContent);
$pass = 'Ladder';
$body = array("aps" => array("alert" => $re_content, "badge" => 1, "sound" => 'received5.caf'));
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'dev.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
//$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
stream_set_blocking ($fp, 0);
if (!$fp)
{
print "Failed to connect $err $errstrn";
return;
}
else
{
print "Connection OK\n<br/>";
}
$payload = json_encode($body);
$msg =
// new: Command "1"
chr(1)
// new: Identifier "1111"
. chr(1) . chr(1) . chr(1) . chr(1)
// new: Expiry "tomorrow"
. pack('N', time() + 86400)
// old
. chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
//print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
//checkAppleErrorResponse($fp);
echo 'Done\n';
fclose($fp);
echo $apple_error_response = fread($fp, 6);
/* return false;
exit(); */
}
?>
I call this function after fwrite and before close commands
function error_response($fp)
{
$read = array($fp);
$null = null;
$changedStreams = stream_select($read, $null, $null, 0, 1000000);
if ($changedStreams === false)
{
echo ("Error: Unabled to wait for a stream availability");
}
elseif ($changedStreams > 0)
{
$responseBinary = fread($fp, 6);
if ($responseBinary !== false || strlen($responseBinary) == 6)
{
$response = unpack('Ccommand/Cstatus_code/Nidentifier', $responseBinary);
var_dump($response);
}
}
}
It works when I sending a notification in the enhanced format. But doesn't work with notification in the simple format. I don't have idea why... Any clue?