【问题标题】:Sending push notification using GCM in PHP在 PHP 中使用 GCM 发送推送通知
【发布时间】:2016-04-28 06:47:06
【问题描述】:

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

【问题讨论】:

标签: php


【解决方案1】:

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

registration_ids" field must be a JSON array

所以

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

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 2015-03-08
    • 2018-02-09
    相关资源
    最近更新 更多