douhuang75397 2014-02-13 21:57 采纳率: 0%
浏览 39
已采纳

来自GCM服务器的401错误[重复]

This question already has an answer here:

Hello Im trying to use GCM on my android app.

My problem is that when trying to sent message I get 401 error, Unauthorized error.

I made a key for server applications in the google cloud console and followed this instructions: http://developer.android.com/google/gcm/gs.html

This my code:

    class GCM {

        //put your code here
        // constructor
        function __construct() {

        }

        /**
         * Sending Push Notification
         */
        public function send_notification($registatoin_ids, $message) {

            // Set POST variables
            $url = 'https://android.googleapis.com/gcm/send';

            $fields = array(
                'registration_ids' => $registatoin_ids,
                'data' => $message,
            );

            $headers = array(
             'Content-Type: application/json',
                'Authorization: key=**MY_KEY***'        
            );
            // 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;
        }

    }

from my app I sent POST to this PHP code:

    include_once './GCM.php';

        $gcm = new GCM();

        $message = array("status" => "1");

    $ids = array('**my_gcm_id**');

        $result = $gcm->send_notification($ids, $message);

    echo $result;
</div>
  • 写回答

2条回答 默认 最新

  • dongshenjie3055 2014-02-14 07:50
    关注

    I removed all allowed ip so any ip is allowed and its working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序