php - GCM 服务器出现 401 错误

标签 php android google-cloud-messaging

您好,我正在尝试在我的 Android 应用程序上使用 GCM。

我的问题是,当尝试发送消息时,我收到 401 错误,未经授权的错误。

我在谷歌云控制台中为服务器应用程序创建了 key ,并按照以下说明进行操作:http://developer.android.com/google/gcm/gs.html

这是我的代码:

    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;
        }

    }

从我的应用程序中,我将 POST 发送到此 PHP 代码:

    include_once './GCM.php';

        $gcm = new GCM();

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

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

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

    echo $result;

最佳答案

我删除了所有允许的 IP,因此任何 IP 都被允许并且可以正常工作。

关于php - GCM 服务器出现 401 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766403/

相关文章:

android - 动态添加项目到 RecyclerView

android - 只有在抽屉导航完成关闭动画后才打开下一个 Activity

android - 如何在应用程序中打开 mainactivity 的 webview,当收到通知时

android - Android 推送通知使用 GCM 是否免费

google-cloud-messaging - FCM 和 GCM 是否向后和向前兼容?

javascript - 将数组值传递给js

javascript - 尝试使用 javascript/PHP 更新数据库不起作用

android - 禁用 ActionBarDrawerToggle 抽屉指示器,但保留汉堡包图标

php - 从 json 字符串中提取数据返回错误

php - MySQL 和 PHP : Sorting text by alphabetical order for English & string length for non-English