douzhicong5965 2015-01-26 05:54
浏览 33

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

i am trying to send the gcm message everything works fine the message is send from server and received in the phone , i am testing the code to send it to more than 1000 users so i am concatenating the count to each message however it just sends 1 as count and not actual count

how can i send the count in gcm messages

i am trying this for testing and i am totally new to 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条回答 默认 最新

  • doulai8405 2015-01-26 05:58
    关注

    You can call the count method of mysql which returns the total row count. I implemented a simple sample, do as you required.

    $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;
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大