向所有用户发送推送通知

时间:2012-03-02 14:42:44

标签: objective-c ios push-notification apple-push-notifications

所以,我有一个应用程序。此应用程序使用此PHP代码发送推送通知:

<?php

$deviceToken = '4bc9b8e71b9......235095a22d';

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

// Put your alert message here:
$message = 'My Message Here!';

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

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

// 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代码,推送通知将仅发送到此设备(4bc9b8e71b9 ...),或者它将被发送对我的所有用户?如果仅将此信息发送到此设备,如何将推送发送给我的所有用户?

PS:我遵循了this教程,它也运行良好,除非因为我不知道Push是否会发送给我的所有用户。

抱歉英文不好,非常感谢 !!

2 个答案:

答案 0 :(得分:2)

通常的方法是在数据库中存储令牌,一旦你需要发送它们 - 只需从数据库中选择令牌并循环它们。

代码可能看起来像

$pdo = new PDO(
    "mysql:host=$db_host;port=$db_port;dbname=$db_name",
    $db_user,
    $db_pass
);  
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);      
$select_tokens_sql = 'SELECT * FROM tokens';
$select_tokens_statement = $pdo->prepare($select_tokens_sql);

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

$select_tokens_statement->execute();
$tokens = $select_tokens_statement->fetchAll();
//loop through the tokens
foreach($tokens as $token) {     

   // Build the binary notification
   $msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) .  $payload;

   // Send it to the server
   $result = fwrite($fp, $msg, strlen($msg));

   if (!$result)
      echo 'Message to the device ' . $token . ' not delivered' . PHP_EOL;
   else
       echo 'Message to the device ' . $token . ' successfully delivered' . PHP_EOL;
}
// Close the connection to the server
fclose($fp);

在您完成发送推送通知后立即收听苹果反馈服务也是一个好主意。它将告诉您是否在某些设备上您的应用程序不再存在,因此您可以安全地从数据库中删除相应的令牌。

答案 1 :(得分:1)

您必须遍历所有设备令牌并使用fwrite()将其写入网关