在PHP中使用GCM发送推送通知

时间:2016-04-28 06:47:07

标签: php

我正在使用以下代码在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)),但它没有用。

1 个答案:

答案 0 :(得分:0)

由于向多个接收者发送消息:

registration_ids" field must be a JSON array

所以

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