user1126886
user1126886

Reputation:

Send Push Notifications to all users

So, I have an App. This app, send a Push Notification using this PHP code:

<?php

$deviceToken = '4bc9b8e71b9......235095a22d';

// Put your private key's passphrase here:
$passphrase = '12345';

// Put your alert message here:
$message = 'My Message 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.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;

// 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);

My question is: If I have more than one users in my app, and I run this PHP code in Terminal, the Push Notification will be send only to this Device (4bc9b8e71b9...), or it'll be send to all of my users? If this will be sent only to this Device, how can I send the Push to all my users?

PS: I followed this tutorial, and it worked as well, except because i dont know if the Push will be send to all my users.

Sorry for the bad english, and thanks a lot!!

Upvotes: 0

Views: 6686

Answers (2)

Alex Petrov
Alex Petrov

Reputation: 275

The usual approach is to store tokens in the database and once you need to send them - just select the tokens from the DB and loop through them.

the code might look like that

$pdo = new PDO(
    "mysql:host=$db_host;port=$db_port;dbname=$db_name",
    $db_user,
    $db_pass
);  
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);      
$select_tokens_sql = 'SELECT * FROM tokens';
$select_tokens_statement = $pdo->prepare($select_tokens_sql);

// 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;

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

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

$select_tokens_statement->execute();
$tokens = $select_tokens_statement->fetchAll();
//loop through the tokens
foreach($tokens as $token) {     

   // Build the binary notification
   $msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) .  $payload;

   // Send it to the server
   $result = fwrite($fp, $msg, strlen($msg));

   if (!$result)
      echo 'Message to the device ' . $token . ' not delivered' . PHP_EOL;
   else
       echo 'Message to the device ' . $token . ' successfully delivered' . PHP_EOL;
}
// Close the connection to the server
fclose($fp);

It might also be a good idea to listen to apple feedback service just after you have finished sending push notifications. It will tell you if on some devices your app is not present anymore so you can safely remove corresponding tokens from the database.

Upvotes: 2

Mark
Mark

Reputation: 2704

You'll have to loop over all of your device tokens and write them to the gateway with fwrite()

Upvotes: 1

Related Questions