简体   繁体   English

使用PHP向所有iOS用户发送推送通知

[英]Sending Push Notification To All iOS Users Of App Using PHP

I'm using this PHP to send out push notifications to just one user of my app. 我正在使用此PHP向我的应用程序的一个用户发送推送通知。 Everything works great with that, but I need to send it to all users: 一切都很好,但是我需要将其发送给所有用户:

$deviceToken = 'DeviceTokenHere';

// Put your private key's passphrase here:
$passphrase = 'Passphrase Here';

// Put your alert message here:
$message = 'There are 2 new requests at the bottom of the list.  Please check them out.';

////////////////////////////////////////////////////////////////////////////////

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

// 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;

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

How would I alter this to send it to all users? 我将如何更改以发送给所有用户?

for ($deviceToken in $allDevices) {
    SendPushToDevice($deviceToken);
}

Note! 注意! If count of $allDevices > 100, then you must do it with some time intervals, otherwise push will not delivered. 如果$ allDevices的计数> 100,则必须每隔一定的时间间隔执行一次,否则将无法推送。

for ($i = 0; $i < count($allDevices); $i++) {
    if ($i > 0 && $i % 100 == 0) {
        // your sleep function
    } else {
        $deviceToken = $allDevices[$i];
        SendPushToDevice($deviceToken);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM