0

To say the good thing first - my code is working. But unfortunately, it is extremly slow with many devices to push to. As the whole pushing process failed when one device failed with my code before (the connection was made outside the loop), I took the connection inside my loop:

foreach ($deviceTokens as $token) {
  $fp = stream_socket_client($this->data['config']['push']['apnsAddress'], $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
  $msg = chr(0).pack('n', 32).pack('H*', $token).pack('n', strlen($payload)).$payload;
  $result = fwrite($fp, $msg);
  if (!$result) {
    $this->pushLog('devicetoken '.$token.' failed');
    $errors++;
  }
  fclose($fp);
}

Isn't there any other way to push to multiple devices than making a new connection for every single person that has an app installed? The runtime for this script is just way to long at the moment.

Michael Kunst
  • 2,978
  • 25
  • 40

1 Answers1

0

First things first, may I know how many devices are you trying to push in the loop? The code you have should work fine for reasonable amounts of tokens. How fast is your server-to-apple connection? An alternative would be to use a third-party push API. Normally, they offer extended parameters and options, which surely include push to multiple devices with a single call. Cheers.

John Doe
  • 385
  • 3
  • 10
  • Even in the testmode with 3 devices it takes almost a second. In live mode we talk about a thousand devices. And a third party solution is not an option for us. – Michael Kunst Sep 16 '13 at 10:41
  • Please see this: http://stackoverflow.com/questions/5050363/how-can-i-send-push-notification-to-multiple-devices-in-one-go-in-iphone – John Doe Sep 16 '13 at 10:44
  • Found a working solution here: http://stackoverflow.com/questions/10058768/php-apple-enhanced-push-notification-read-error-response Still not a beautiful solution though.. – Michael Kunst Sep 16 '13 at 15:45