How to send Push Notification to multiple devices?
I found that you have to create a new stream_context_create for each fwrite to prevent apple from closing the connection for a bad token.
Try this example code and modify for your environment.
$apnsHost = '<APNS host>';
$apnsPort = <port num>;
$apnsCert = '<cert>';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array('alert' => 'some notification', 'badge' => 0, 'sound' => 'none');
$payload = json_encode($payload);
// Note: $device_tokens_array has list of 5 devices' tokens
for($i=0; $i<5; $i++)
{
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_tokens_array[i])) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
}?>
This article helps verifying drop connection and connection status: Apple Push Notification: Sending high volumes of messages
Other reference links:
How can I send push notification to multiple devices in one go in iPhone? and how to handle multiple devices when using Push Notification?