dtgu21994537 2017-03-04 04:43
浏览 54
已采纳

使用PHP将GCM通知发送到多个设备

I need to send GCM notifications to multiple devices. Here i made PHP code to get the array of Registration ID from MySql and tried to send notification to multiple devices but there is some problem here.

My PHP Code:

<?php 
if($_SERVER['REQUEST_METHOD']=='GET'){

$tags  = $_GET['tags'];
 $api_key = 'My OWN API KEY';   

//Getting registration token we have to make it as array 

//Getting the message 
$message = Testing GCM';
$title= 'Cuboid';
$vibrate= '1';
$sound= '1';


require_once('dbConnect.php');

$user_ids = array();

       foreach ($_REQUEST['tags'] as $key => $val) {
       $user_ids[$key] = filter_var($val, FILTER_SANITIZE_STRING);
        }
        $tagss = "'" . implode("','", $user_ids) . "'";
           $sql = "SELECT user_tags.user_id AS userID , gcm_token.regtoken AS regToken
                   FROM user_tags,gcm_token
                    WHERE tags IN ({$tagss}) AND user_tags.user_id=gcm_token.user_id";


$r = mysqli_query($con,$sql);

 //creating a blank array 
 $result = array();
 $reg_token = array();


 //looping through all the records fetched

    while ($row = mysqli_fetch_array($r)) {
    $result['regToken'][] = $row['regToken'];   
      }

     //Displaying the array in json format 

     echo json_encode(array('result'=>$result));

     $reg_token = (json_encode(array('result'=>$result)));
     //$reg_token = array('result'=>$result);


     $msg = array
(
    'message'   => $message,
    'title'     => $title,
    'subtitle'  => 'Android Push Notification using GCM Demo',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => $vibrate,
    'sound'     => $sound,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);

//Creating a new array fileds and adding the msg array and registration token array here 
$fields = array
(
    'registration_ids'  => $reg_token,
    'data'          => $msg
);

//Adding the api key in one more array header 
$headers = array
(
    'Authorization: key=' . $api_key,
    'Content-Type: application/json'
); 

//Using curl to perform http request 
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );

//Getting the result 
$results = curl_exec($ch );
curl_close( $ch );

//Decoding json from results 
$res = json_decode($results);

      mysqli_close($con);
    }
  • 写回答

2条回答 默认 最新

  • doumo2501 2017-03-04 09:30
    关注

    You must get result in JSON Array. Try the following code:

    <?php
    
    if($_SERVER['REQUEST_METHOD']=='GET'){
    
    $tags  = $_GET['tags'];
    // Replace with the real server API key from Google APIs
    $apiKey = "YOUR_API_CODE";
    
    $message = "Hello Raja";
    
    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';
    
    
    require_once('dbConnect.php');
    
    $user_ids = array();
    
           foreach ($_REQUEST['tags'] as $key => $val) {
           $user_ids[$key] = filter_var($val, FILTER_SANITIZE_STRING);
            }
            $tagss = "'" . implode("','", $user_ids) . "'";
               $sql = "SELECT user_tags.user_id AS userID , gcm_token.regtoken AS regToken
                       FROM user_tags,gcm_token
                        WHERE tags IN ({$tagss}) AND user_tags.user_id=gcm_token.user_id";
    
    $r = mysqli_query($con,$sql);
    
    $result = array();
    
    
    
     //looping through all the records fetched
    
      while ($row = mysqli_fetch_array($r)) {
        $result[] = $row['regToken'];   
    }
    
     //Displaying the array in json format 
    
       //echo json_encode(array('result'=>$result));
    
         echo json_encode(($result));
    
                $registrationIDs = ($result);
    
    
        //echo json_encode(array('result'=>$result));
    
    
    $fields = array(
        'registration_ids' => $registrationIDs,
        'data' => array( "message" => $message ),
    );
    $headers = array(
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
    );
    
    // Open connection
    $ch = curl_init();
    
    // Set the URL, number of POST vars, POST data
    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));
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // curl_setopt($ch, CURLOPT_POST, true);
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));
    
    // Execute post
    $result = curl_exec($ch);
    
    // Close connection
    curl_close($ch);
    echo $result;
    //print_r($result);
    //var_dump($result);
          }
    
         ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误