0

我使用推送通知方法创建了一个应用程序。一切正常。我可以在我的 iPhone 上收到推送通知。我创建了一个示例 Web 应用程序来发送推送通知。现在,我为设备令牌列表创建了一个数据源。我希望我的 Web 应用程序向许多设备(设备列表)发送通知,这就是为什么我在 MySql 数据库中放置一个包含设备令牌列表的示例表。我的问题是,如何一次向多个设备发送推送通知?

这是我的 php Simplepush.php 代码

<?php

// Put your device token here (without spaces):
$deviceToken = $_POST['devicetoken'];

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

// Put your alert message here:
$message = $_POST['message'];

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

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

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

  ?>

使用设备令牌列表的推送通知代码

<?php
 $con=mysqli_connect("localhost", "xxx", "xxxxx","xxxxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

$result = mysqli_query($con,"SELECT * FROM push");

  echo "<table border='1'>
  <tr>
    <th>id</th>
    <th>Device Token</th>
 </tr>";

 while($row = mysqli_fetch_array($result))
{

echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['token'] . "</td>";
echo "</tr>";

$deviceToken = $row['token'];
$passphrase = '123jakes123@';
$message = $_POST['message'];

}
echo "</table>";
////////////////////////////////////////////////////////////////////////////////



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

// 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);
mysqli_close($con);
?>
4

0 回答 0