2016-04-28 107 views
1

我正在使用以下代碼在PHP中使用GCM發送推送通知消息,但我無法讓它工作。我嘗試這樣做:在PHP中使用GCM發送推送通知

/*------send_messages_gcm--------*/ 
    private function send_messages_gcm(){ 
     global $wpdb; 
     include_once './config.php'; 

     $GOOGLE_API_KEY= "AIzaSyD-8NPnZ3WpdMIc-9TtKPCMRQtcliykc-s"; 
$registatoin_ids=$_REQUEST['rj_id']; 

     $message="hii anita"; 
     // Set POST variables 
     $url = 'https://android.googleapis.com/gcm/send'; 

     $fields = array(
      'registration_ids' => $registatoin_ids, 
      'data' => $message, 
     ); 
     // print_r($fields); 
     $headers = array(
      'Authorization: key=' .$GOOGLE_API_KEY, 
      '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); 

     // Disabling SSL Certificate support temporarly 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

     // Execute post 
     $result = curl_exec($ch); 
     if ($result === FALSE) { 
      die('Curl failed: ' . curl_error($ch)); 
     } 

     // Close connection 
     curl_close($ch); 
     echo $result; 

    } 

當運行該函數然後它產生這樣的結果:

registration_ids」字段不是JSON陣列

我還試圖curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode());代替curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)),但它不起作用。

+0

和u得到的'的var_dump($ _ REQUEST [ 'rj_id'])'什麼R'或'var_dump($ fields)'? – devpro

+0

你好這可能可以幫助你http://mitalichokshi.blogspot.in/和http://mitalichokshi.blogspot.in/2015/05/how-to-send-bulk-push-notifications-to.html – JiteshNK

+0

我是從var_dump($ _ REQUEST ['rj_id'])獲取設備ID,並且字段給出推送通知消息 –

回答

0

由於發送消息給多個接收器:

registration_ids" field must be a JSON array 

所以

$registatoin_ids = array($_REQUEST['rj_id']);