php - 我们如何从 PHP 服务器向数千台设备(注册 ID)发送推送通知

标签 php android google-cloud-messaging

我们已经设置了 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/

相关文章:

PHP 流/文件上传和 max_input_vars

php - android中发送json对象数据到远程mysql数据库

android - 通过 TextView 打开默认短信应用程序

android - 如何在 Chrome(Android、iOS)中删除 input[type=date] 中的左缩进

android - GCM 和 App 更新 : how re-register if app is not open after update?

php - 从 wsdl 生成 php 代码

php - FedEx shipping api 在印度国内有效吗

android - 在 Android 中录制音频时捕获时间戳

安卓 : GCM Push Notification to particular device

ios 使用 GCM 设备向设备发送推送通知