【问题标题】:GCM is not working on live serverGCM 无法在实时服务器上运行
【发布时间】:2014-03-27 02:47:39
【问题描述】:

我已经使用 GCM 实现了推送通知。当我运行必要的文件以从本地服务器(在我的计算机上使用XAMPP 服务器)向设备发送消息时,一切都运行良好。但是当我将文件上传到我的实时服务器并运行文件时,我没有收到推送通知。我验证了所有文件,但没有得到。以下是我的php 代码:

send_message.php

<?php

if (isset($_REQUEST["regId"]) && isset($_REQUEST["message"]) && isset($_REQUEST["senderid"])) {
    $regId = $_REQUEST["regId"];
    $message = $_REQUEST["message"];
    $senderid = $_REQUEST["senderid"];


    include_once './GCM.php';

    $gcm = new GCM();

    $registatoin_ids = array($regId);

    $message = array("message" => $message, "sender_id" => $senderid);
    $result = $gcm->send_notification($registatoin_ids, $message);
    echo $result;die;

}

?>

GCM.php

<?php

    class GCM {

        function __construct() {

        }

        public function send_notification($registatoin_ids, $message) {
            // include config
            include_once './config.php';


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

            $fields = array(

                'registration_ids' => $registatoin_ids,
                'data' => $message, 

            );

            $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';
            echo json_encode($fields);
            echo $result;
        }

    }

    ?>

我正在通过提供服务器 url 和必要的参数从浏览器执行 send_message.php 文件。在本地机器上,我收到推送通知,但是当我通过提供实时服务器url 执行相同的文件时,它没有给我任何输出。可能是什么问题?

【问题讨论】:

  • Anil,你能从 live url 访问吗?我的意思是,当您将代码放到实时服务器时,您可以使用实时 url 访问它吗?
  • 是的,我可以访问它...
  • 好,你能告诉我那个网址吗?
  • @Kedarnath:我认为,这里的问题出在 GCM.php 文件的 curl 部分...
  • 是的,我认为 curl 在 XAamp 中有一些设置可以从实时位置打开它。

标签: android google-cloud-messaging


【解决方案1】:

问题可能出在您用于向 Android 设备发送推送通知的 API 密钥上。

Google Developers Console 中提供了两种 API 密钥

使用服务器密钥 如果您的应用程序在服务器上运行

使用浏览器密钥 如果您的应用程序在客户端上运行,例如来自 JavaScript 或任何其他客户端网络浏览器上下文。

以下是开发者控制台的屏幕截图,您可以在其中获得浏览器和服务器 API 密钥。

【讨论】:

    【解决方案2】:

    当您想从实时地址访问 PHP 页面时,您只需要从您的 XAMPP 启用cURL

    转到您的 php.ini 文件并取消注释以下行(删除 ; 会将其从评论中删除),

    ;extension=php_curl.dll
    

    完成此操作后,您可以在 phpinfo() 中检查 curl,如下图所示,

    对于LAMP

    来自http://buzznol.blogspot.com/2008/12/install-curl-extension-for-php-in.html

    sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
    

    安装 libcurl 后,您应该使用以下命令之一重新启动 Web 服务器,

    sudo /etc/init.d/apache2 restartsudo service apache2 restart

    【讨论】:

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