向所有用户发送推送通知 [英] Send Push Notifications to all users

查看:26
本文介绍了向所有用户发送推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, I have an App. This app, send a Push Notification using this PHP code:

<?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...),否则将发送我所有的用户?如果这将仅发送到此设备,我如何将推送发送给我的所有用户?

My question is: If I have more than one users in my app, and I run this PHP code in Terminal, the Push Notification will be send only to this Device (4bc9b8e71b9...), or it'll be send to all of my users? If this will be sent only to this Device, how can I send the Push to all my users?

PS:我遵循了教程,它也能正常工作,只是因为我不知道推送是否会发送给我的所有用户.

PS: I followed this tutorial, and it worked as well, except because i dont know if the Push will be send to all my users.

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

推荐答案

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

The usual approach is to store tokens in the database and once you need to send them - just select the tokens from the DB and loop through them.

代码可能看起来像那样

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

在您发送完推送通知后立即收听 Apple 的反馈服务也可能是一个好主意.它会告诉您是否在某些设备上您的应用不再存在,以便您可以安全地从数据库中删除相应的令牌.

It might also be a good idea to listen to apple feedback service just after you have finished sending push notifications. It will tell you if on some devices your app is not present anymore so you can safely remove corresponding tokens from the database.

这篇关于向所有用户发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆