4

I want to send same messages to all devices who are registered with application but how can send them without making multiple connections...

My current PHP code:

ctx = stream_context_create();  
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');  
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
if (!$fp)
{
  print "Failed to connect $err $errstr\n";
  return;
}
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
fwrite($fp, $msg);
Prabh
  • 2,466
  • 2
  • 24
  • 27
  • Could you format your code a little better by [putting 4 whitespaces](http://stackoverflow.com/editing-help) before each code line? Right now it's not readable. – Darin Dimitrov Feb 19 '11 at 10:58
  • I hope it's readable now... :) – Prabh Feb 19 '11 at 11:17
  • @Prabh -- can you show me the final code for how you solved this issue? Im trying to achieve the same thing -- thanks! – tamak Mar 09 '15 at 13:43
  • Hi Tamak, it was long back so I dont have the code but I used for loop at tht time to send msg to all devices :( – Prabh Mar 12 '15 at 00:59

2 Answers2

10

Bottom line, you can't. You need to send a message to each token.

Its not working like a email where you can have multiple recipients.

Once the connection is open you can send a bunch of messages, thats also the preferred way (based on Apples SDK).

from the SDK:

http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW2

You should also retain connections with APNs across multiple notifications. APNs may consider connections that are rapidly and repeatedly established and torn down as a denial-of-service attack. Upon error, APNs closes the connection on which the error occurred.

Roger
  • 7,535
  • 5
  • 41
  • 63
  • new to PHP here, does it mean you create your connection once like $fp = stream_socket_client( 'ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); while you can `fwrite($fp, $msg);` multiple times (even 1000 times)? – Darpan Aug 21 '16 at 07:52
  • Link is defunct – Darpan Oct 14 '16 at 22:58
3

You can use one connection to send multiple messages, so you don't need to open multiple connections. You can't use one message for multiple devices.

Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95
  • Bolhoed, Can you tell me how? – Prabh Feb 22 '11 at 09:58
  • 1
    Just do multiple fwrite's on the same connection. – Kevin Renskers Feb 23 '11 at 18:41
  • Hi @mixedCase doing multiple fwrites (ie 100+) didn't work for me, many of them did not get my push, is that possible? Should I do anything else If users to send push are 1000+? – Darpan Aug 21 '16 at 07:54
  • Multiple writes works but if one of the devices is wrong, apple will not send any other connections, how to deal with it? ...and it doesnt terminate the connection so you dont know which one is wrong – Srneczek Nov 26 '16 at 11:46