2017-03-04 77 views
2

我需要將GCM通知發送到多個設備。在這裏,我讓PHP代碼從MySql獲取註冊ID數組,並嘗試向多個設備發送通知,但這裏存在一些問題。使用PHP將GCM通知發送到多個設備

我的PHP代碼:

<?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); 
    } 
+0

每個請求有1000個陣列的限制。 –

+0

是的,但我沒有收到任何通知。爲什麼?我試圖發送單個令牌的陣列 – iamRaja

+0

我Ony有6個Reg_Ids? –

回答

1

您必須在JSON數組中獲得結果。請嘗試以下代碼:

<?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); 
     } 

    ?> 
+0

謝謝你的工作.. – iamRaja

2

更新:

按照該updated documentation的API端點發送推送通知從 https://android.googleapis.com/gcm/send 改爲https://fcm.googleapis.com/fcm/send

老端點仍然有效,但我會推薦使用最新的。 這兩個端點的代碼保持不變。


試試這個, 聲明一個函數來發送通知:

function sendfcmMessage($registrationIds, $msg) 
{ 

    if (!defined('API_ACCESS_KEY')) define('API_ACCESS_KEY', '<PUT_YOUR_KEY_HERE>'); 
    $fields = array('registration_ids' => $registrationIds, 'data' => $msg, 'content-available' => 1, 'priority' => 'high'); //set priority as required 
    $headers = array('Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json'); 
    $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)); 
    $result = curl_exec($ch); 
    curl_close($ch); 
    return $result; 
} 

使用它像這樣,

$msg = array('message' => $message, 
      'title' => $title, 
      'body' => $body, 
      'subtitle' => $subtitle, 
      'tickerText' => $ticker_text, 
      'vibrate' => $vibrate, 
      'sound' => $sound, 
      'largeIcon' => 'large_icon', 
      'smallIcon' => 'small_icon'); 

    $registrationIds = array(); 

    while ($row = mysqli_fetch_array($r)) { 
     array_push($registrationIdsIOS, $row['regToken'];); 
    } 

    sendfcmMessage($registrationIds, $msg); 

您也可以通過打印這個功能對結果進行調試知道通知是否被髮送,它也讓你知道爲什麼沒有發送通知。

echo sendfcmMessage($registrationIds, $msg); 
相關問題