首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PHP脚本向10万用户发送GCM通知

用PHP脚本向10万用户发送GCM通知
EN

Stack Overflow用户
提问于 2014-06-22 18:25:54
回答 1查看 1.2K关注 0票数 0

我有一个基于GCM通知的应用程序,每次新用户安装它的GCM注册id都存储在php服务器的mysql数据库中,现在我已经编写了一个脚本,向所有注册用户发送通知,如下所示

代码语言:javascript
复制
<?php

//------------------------------
// Payload data you want to send 
// to Android device (will be
// accessible via intent extras)
//------------------------------


//------------------------------
// The recipient registration IDs
// that will receive the push
// (Should be stored in your DB)
// 
// Read about it here:
// http://developer.android.com/google/gcm/
//------------------------------

if(isset($_GET['message'])) 
{

    $msg = $_GET['message'];
    $data = array("price" => $msg);
    $ids = array();
    require_once dirname(__FILE__). '/db_connect.php';;
    $db = new DB_CONNECT();
    $qry = mysql_query("SELECT gcm_regid FROM gcm_users");



    while($row = mysql_fetch_array($qry))
    {
        $ids[] = $row['gcm_regid'];
    }

//$ids = array( 'abc', 'def' );

//------------------------------
// Call our custom GCM function
//------------------------------


//------------------------------
// Define custom GCM function
//------------------------------

function sendGoogleCloudMessage( $data, $ids )
{
    //------------------------------
    // Replace with real GCM API 
    // key from Google APIs Console
    // 
    // https://code.google.com/apis/console/
    //------------------------------

    $apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

    //------------------------------
    // Define URL to GCM endpoint
    //------------------------------

    $url = 'https://android.googleapis.com/gcm/send';

    //------------------------------
    // Set GCM post variables
    // (Device IDs and push payload)
    //------------------------------

    $post = array(
                    'registration_ids'  => $ids,
                    'data'              => $data,
                    );

    //------------------------------
    // Set CURL request headers
    // (Authentication and type)
    //------------------------------

    $headers = array( 
                        'Authorization: key='. $apiKey,
                        'Content-Type: application/json'
                    );

    //------------------------------
    // Initialize curl handle
    //------------------------------

    $ch = curl_init();

    //------------------------------
    // Set URL to GCM endpoint
    //------------------------------

    curl_setopt( $ch, CURLOPT_URL, $url );

    //------------------------------
    // Set request method to POST
    //------------------------------

    curl_setopt( $ch, CURLOPT_POST, true );

    //------------------------------
    // Set our custom headers
    //------------------------------

    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );

    //------------------------------
    // Get the response back as 
    // string instead of printing it
    //------------------------------

    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

    //------------------------------
    // Set post data as JSON
    //------------------------------

    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );

    //------------------------------
    // Actually send the push!
    //------------------------------

    $result = curl_exec( $ch );

    //------------------------------
    // Error? Display it!
    //------------------------------

    if ( curl_errno( $ch ) )
    {
        echo 'GCM error: ' . curl_error( $ch );
    }

    //------------------------------
    // Close curl handle
    //------------------------------

    curl_close( $ch );

    //------------------------------
    // Debug GCM response
    //------------------------------

    echo $result;
}
sendGoogleCloudMessage(  $data, $ids );

}
?>

我担心的是,当用户基数很大时,脚本可能会失败。

EN

回答 1

Stack Overflow用户

发布于 2014-06-22 19:19:09

如果您在同一个请求中向GCM发送超过1000个注册in,您的脚本将失败。必须确保每个请求最多包含1000个注册ID。

registration_ids接收消息的设备列表(注册ID)的字符串数组。它必须包含至少1和最多1000个注册ID。要发送多播消息,必须使用JSON。为了向单个设备发送一条消息,您可以使用一个JSON对象,它只有一个注册id或纯文本(参见下面)。请求必须包括收件人--可以是注册ID、注册ID数组,也可以是notification_key。必填项。

(来源)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24354337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档