簡體   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