This is not my first app sending push notifications but it is my first app where I send a notification to all my users at the same time.
What Im experiencing is that not all my users receive the notification, only some of them, even though they have the settings correct(ie notifications for my app enabled), also the code is correct since it is sent to some of them, so my guess is that the code "misses" some executions, since the conection with the APNS is asyncronous so somehow( you tell me if im wrong) that it messes the queue of sending notifications.
Here's the code:
function sendNotification(){
$sql = "SELECT * FROM users WHERE phone = 'iPhone' AND pushID != ''";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_OBJ);
$request = Slim::getInstance()->request();
$content = json_decode($request->getBody());
$message = $content->message;
foreach($users as $user){
// Put your device token here (without spaces):
$deviceToken = $user->pushID;
// Put your private key's passphrase here:
$passphrase = 'xxxxxxx';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxxxx.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' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
}
$db = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
As you can see I fetch all my users with an iPhone and I send them the notification. My phone is the last user in that list and I didnt get it, another user that I know is one of the first ones and she got it. It either misses some of the users in the array or it just does the first half, no error is shown.
Im using Slim for this.
hope you can help me, thanks!