2016-04-28 110 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']);