【问题标题】:send gcm message in groups of 1000 users to all 10000 users以 1000 个用户为一组向所有 10000 个用户发送 gcm 消息
【发布时间】:2015-01-24 18:01:09
【问题描述】:

我正在使用以下代码使用 php 和 mysqldb 发送 gcm 消息我已成功发送 gcm 消息并在设备上接收到它,但是 android 指南规定 gcm 消息应分批发送 1000 个 http://developer.android.com/training/cloudsync/gcm.html#multicast

现在我的问题是我们如何将大量 1000 条 gcm 消息发送到一个包含 10,000 个注册用户的数据库。

<?php
require 'connect.php';

function sendPushNotification($registration_ids, $message) {

    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registration_ids,
        'data' => $message,
    );

    define('GOOGLE_API_KEY', 'keys_value');

    $headers = array(
        'Authorization:key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    echo json_encode($fields);
    $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_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if($result === false)
        die('Curl failed ' . curl_error());

    curl_close($ch);
    return $result;

}

$pushStatus = '';

if(!empty($_GET['push'])) {

    $query = "SELECT gcm_regId FROM gcm_users";
    if($query_run = mysql_query($query)) {

        $gcmRegIds = array();
        while($query_row = mysql_fetch_assoc($query_run)) {

            array_push($gcmRegIds, $query_row['gcm_regId']);

        }

    }
    $pushMessage = $_POST['message'];
    if(isset($gcmRegIds) && isset($pushMessage)) {

        $message = array('price' => $pushMessage);
        $pushStatus = sendPushNotification($gcmRegIds, $message);

    }   
}

?>

<html>
    <head>
        <title>Google Cloud Messaging (GCM) Server in PHP</title>
    </head>
    <body>
    <h1>Google Cloud Messaging (GCM) Server in PHP</h1>
    <form method = 'POST' action = 'send_all.php/?push=1'>
        <div>
            <textarea rows = 2 name = "message" cols = 23 placeholder = 'Messages to Transmit via GCM'></textarea>
        </div>
        <div>
            <input type = 'submit' value = 'Send Push Notification via GCM'>
        </div>
        <p><h3><?php echo $pushStatus ?></h3></p>
    </form>
    </body>
</html>

【问题讨论】:

标签: php android google-cloud-messaging


【解决方案1】:

将 $gcmRegIds 设为 2D 数组,然后对其进行 foreach 推送消息:

if(!empty($_GET['push'])) {

    $query = "SELECT gcm_regId FROM gcm_users";
    if($query_run = mysql_query($query)) {

        $gcmRegIds = array();
        $i = 0;
        while($query_row = mysql_fetch_assoc($query_run)) {
            $i++;
            $gcmRegIds[floor($i/1000)][] = $query_row['gcm_regId'];
        }

    }
    $pushMessage = $_POST['message'];
    if(isset($gcmRegIds) && isset($pushMessage)) {

        $message = array('price' => $pushMessage);
        $pushStatus = array();
        foreach($gcmRegIds as $val) $pushStatus[] = sendPushNotification($val, $message);

    }   
}

【讨论】:

  • 我对 php 很陌生,你能给出完整的代码吗,请解释一下它是如何工作的
  • 这只会向 1000 个用户发送消息,之后它会停止向所有 10000 个用户发送消息
  • 你如何执行`$pushStatus[] = sendPushNotification($val, $message);`来发送消息?
【解决方案2】:

这是我为解决 FCM 的场景所做的。您也可以忽略代码的某些部分

<?php

$saved_tokens=array(); // could be more than 1000 token the maximum size as per google FCM requirement
//$count=1;
$ID_SETS = array_chunk($saved_tokens, 1000); // make a  group of 1000 items in each set
foreach ($ID_SETS as $value_ids) {
    // echo "Chunk  ".$count."<br>";
    sendFCM_Notification("what_ever_parameter_data",$value_ids);

    foreach ($value_ids as $value_i ) { // you can remove this part its not neccessary
        //echo " ---- contents of Chunk ".$count ."with Value".$value_i."<br>";

    }
    // $count++;    

}
function sendFCM_Notification($what_ever_parameter,$reg_ids_array){

    $url = 'https://fcm.googleapis.com/fcm/send';
        $fields = array(
             'registration_ids' => $reg_ids_array,
             'data' => $what_ever_parameter
            );

        $headers = array(
            'Authorization:key = YOUR_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_SSL_VERIFYHOST, 0);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
       $result = curl_exec($ch);           
       if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);
       return $result;

}



?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 2016-08-26
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多