繁体   English   中英

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

[英]Sending push notification using GCM in 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)) ,但它不起作用。

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

registration_ids" field must be a JSON array

所以

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM