Sending push notifications to many devices

This is my first time posting on stack overflow so I do apologize if this question is extremely obvious.

I am in the process of setting up push notifications for my iOS app and have successful done so with just my own device. I will be testing soon on many devices but I want to know if I have the right idea.

I will keep all the device tokens in my database and when it is time to send a push notification, I simply run through all the rows in my database (that contain tokens) and send them via the follow script, replacing the device token each time and keeping my own private key the same each time. Is this correct?

Thanks in advance!

           // Put your device token here (without spaces):
  $deviceToken = $device; // this will be run through a loop that 
                             changes this variable for every new device


   $passphrase = 'XXXXXXXXX'; //This is mine and remains always
                                the same despite which device... correct?

   // Put your alert message here:
   $message = 'Testing first notification!';

  $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',
'badge' => 1
);

   // 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);
链接地址: http://www.djcxy.com/p/57424.html

上一篇: 使用Api使用推送通知

下一篇: 将推送通知发送到许多设备