My ta开发者_如何学运维sk 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?
Or, you can do it for free using apns-php
http://code.google.com/p/apns-php/
- A nice easy to use APNS class which you can queue up a bunch of messages and send them at once with.
My client currently pays a fortune for UA and we're moving away from them.
精彩评论