My task is sending push notification to multiple users which is working fine with following function
function push_to_apns_badge($token_array)
{
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$pem_path = MODULE_DIR.'push_notification'.DIRECTORY_SEPARATOR;
$apnsCert = $pem_path.'apns-dev.pem';
if(is_array($token_array) && count($token_array))
{
foreach ($token_array as $token)
{
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$badge_count = (int)$token['badge'];
$payload = get_payload_badge($badge_count);
$apnsMessage = get_one_message_to_write_badge($token['token'],$payload);
$wrt = fwrite($apns, $apnsMessage);
$_error_str .= " $error $errorString ";
socket_close($apns);
fclose($apns);
}
}
return array(true,$_error_str);
}
But it will take too much cpu usage.
Can I improve Performance somehow? or something missing there?
Thanks
You are making too many connections to Apple and they could block you if you do it this way.
I suspect the creating and tearing down of the connection with each message is also your slow point there!
Apple recommend opening a connection, sending your messages, leaving it open for as long as you can, and to send future messages down that same connection too.
You may want to look at Urban Airship which will send 1 million messages a month free?