1


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

例如,我通常使用这个 PHP 网络服务来发送一个通知:

$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));

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

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

伙计们,我将等待您的答复。我希望这段代码也能帮助实现服务器端发送普通推送通知的人。提前致谢!!

4

0 回答 0