从包含许多设备的网页推送通知(设备令牌)

时间:2013-08-07 01:01:20

标签: iphone ios ipad

我使用推送通知方法创建了一个应用程序。一切都很好。我可以在iPhone上获得推送通知。我创建了一个示例Web应用程序来发送推送通知。现在,我为List of Device令牌创建了一个数据源。我希望我的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);
?>

0 个答案:

没有答案