批量iOS推送通知

时间:2012-05-30 08:25:07

标签: ios apple-push-notifications

首先感谢您的关注。这里的方案是我需要向成千上万的设备发送相同的PUSH通知。我一直在阅读关于这个主题的Apple文档,我发现我必须批量多个通知。 理论很清楚,但我对实施有疑问。

例如,我通常使用此PHP Web服务发送一个通知:

$deviceToken = 'f9082e0e26fe72f4c9ef04bff33b8274186b51abeecb86e4a4e7ac773081****';
$passphrase = '********';
$message = "Hi, world";
$badge = 2;

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array('alert' => $message, 'sound' => 'default', 'badge'=>$badge);
$body['mis_parametros'] = array('hora' => date("F j, Y, g:i a"));

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload))  .$payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL. ' to device token: '. $deviceToken;

// Close the connection to the server
fclose($fp);

我看不出我应该如何批量发送它们。我应该打电话:

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

然后更改deviceToken并再次调用

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

最后,在所有通知之后,关闭与APN的连接调用??

// Close the connection to the server
fclose($fp);

我会等你的回答,伙计们。我希望这段代码也可以帮助那些正在实现服务器端的人发送普通的PUSH通知。 在此先感谢!!

0 个答案:

没有答案