使用 APNS 向许多 Ios 设备发送推送(获取数组中的令牌)

sending push to many Ios device using APNS ( get token in array )

这是我的 Push.php,它需要一个令牌,我希望它带有一组令牌,因为我想将它发送给许多 Ios 设备

任何帮助请

      <?php

// Put your device token here (without spaces):
$deviceToken ='7b4bf977c7d0bf2b0d5532e972b985390c20708e419902738615869d4b9e5e45';

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

// Put your alert message here:
$message = 'My first push notification!';

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

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'C*****.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'
);

// Encode the payload as JSON
$payload = json_encode($body);
echo $payload;
// 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));
echo $result;
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

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

如你所见,我只发送一个令牌,我想发送一组令牌, 我如何更改此代码以使其成为一个接受令牌数组

的函数

非常感谢

<?php

// STORE A ARRAY OF DEVICE TOKENS 
$$deviceToken_array = [  '7b4bf977c7d0bf2b0d5532e972b985390c20708e419902738615869d4b9e5e43', '7b4bf977c7d0bf2b0d5532e972b985390c20708e419902738615869d4b9e5e44','7b4bf977c7d0bf2b0d5532e972b985390c20708e419902738615869d4b9e5e45'  ];

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

// Put your alert message here:
$message = 'My first push notification!';

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

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'C*****.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'
);


function send_messages(&$fp, $body, $deviceToken_array) {

     foreach($deviceToken_array as $deviceToken) {

     // Encode the payload as JSON
              $payload = json_encode($body);
              echo $payload;
              // 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));
              echo $result;
              if (!$result)
                 echo 'Message not delivered' . PHP_EOL;
              else
                 echo 'Message successfully delivered' . PHP_EOL;

     }

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

//CALL FUNCTION TO SEND MESSAGES TO ALL DEVICES STORED INTO "$deviceToken_array" ARRAY
send_messages(&$fp, $body, $deviceToken_array)