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

时间:2015-12-25 12:21:27

标签: php ios arrays push-notification token

这是我的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);

所以当你看到我只发送一个令牌时,我想发送一个令牌数组, 我怎么能改变这个代码,使它成为一个带有令牌数组

的函数

非常感谢

1 个答案:

答案 0 :(得分:0)

<?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)