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

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

问题描述

所以,我有一个应用程序。这个应用程序,使用该PHP code发送推送通知:

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 code,推送通知将只发送到这个设备(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.

对不起,我的英语不好,和非常感谢

Sorry for the bad english, and thanks a lot!!

推荐答案

通常的做法是存储令牌在数据库中,一旦你需要向他们发送 - 只是通过他们选择的DB和循环令牌

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.

在code可能看起来像

the code might look like that

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

这也可能是一个好主意,你已经完成发送推送通知后立即听取反馈意见的苹果服务。它会告诉你,如果在某些设备上的应用无法present了,所以你可以放心地从数据库中删除相应的标记。

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天全站免登陆