我们已经设置了 GCM 客户端,使用 PHP 脚本发送推送通知的功能也可以正常工作。 现在下一个任务是 Android APP 有数以千计的注册设备。
向所有这些设备发送推送通知的正确方法是什么?
我正在使用 PHP 网络服务器 (HTTP)。 我希望我的问题足够清楚。
用于发送推送通知的工作脚本
<?php
// please enter the api_key you received from google console
$api_key = "proper-api-key";
$name = $_POST['name'];
$deal = $_POST['deal'];
$valid = $_POST['valid'];
$address = $_POST['address'];
// please enter the registration id of the device on which you want to send the message
$registrationIDs= array("Few registrationIDs");
$message = array("name" => $name, "deal" => $deal, "valid" => $valid, "address" => $address);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $api_key,
'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
最佳答案
我得到了官方文档的链接。 http://developer.android.com/training/cloudsync/gcm.html#multicast
这里明确规定了上述问题的处理方式。所以这个问题现在有了答案! :)
Send Multicast Messages Efficiently One of the most useful features in GCM is support for up to 1,000 recipients for a single message. This capability makes it much easier to send out important messages to your entire user base. For instance, let's say you had a message that needed to be sent to 1,000,000 of your users, and your server could handle sending out about 500 messages per second. If you send each message with only a single recipient, it would take 1,000,000/500 = 2,000 seconds, or around half an hour. However, attaching 1,000 recipients to each message, the total time required to send a message out to 1,000,000 recipients becomes (1,000,000/1,000) / 500 = 2 seconds. This is not only useful, but important for timely data, such as natural disaster alerts or sports scores, where a 30 minute interval might render the information useless
关于php - 我们如何从 PHP 服务器向数千台设备(注册 ID)发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21847089/