在消息本身中发送gcm消息的计数

时间:2015-01-26 05:54:07

标签: php android google-cloud-messaging

我正在尝试发送gcm消息一切正常,消息是从服务器发送并在手机中收到的,我正在测试代码将其发送给超过1000个用户,所以我将计数连接到每个消息但是它只发送1作为计数而不是实际计数

如何在gcm消息中发送计数

我正在尝试这个测试,我对php来说是全新的

<?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', 'gcmcode');

    $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();
        $i = 0;
        while($query_row = mysql_fetch_assoc($query_run)) {
            $i++;
            $gcmRegIds[floor($i/1000)][] = $query_row['gcm_regId'];
echo $i . "</br>" ; 
            }
        }      


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


        $pushStatus = array();
$j = 0;
        foreach($gcmRegIds as $val) 
{$j++;
$message = array('price' => $j . $pushMessage);
        $pushStatus[] = sendPushNotification($val,$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>

1 个答案:

答案 0 :(得分:0)

您可以调用count mysql方法,该方法返回总行数。我实现了一个简单的示例,按您的要求执行。

$query = "SELECT count(gcm_regId) as total FROM gcm_users";
while($query_row = mysql_fetch_assoc($query_run)) {
      $total = $query_row['total'] / 1000;

}  

function getGCMCount(){
 $total = "";
 $query = "SELECT count(gcm_regId) as total FROM gcm_users";
    while($query_row = mysql_fetch_assoc($query_run)) {
          $total = $query_row['total'] / 1000;     
    }  

    return $total;
}

function sendPushNotification($registration_ids, $message) {

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

    define('GOOGLE_API_KEY', 'gcmcode');

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

}