【问题标题】:Android phonegap send push notification using Google Cloud Messaging using phpAndroid phonegap 使用 php 使用 Google Cloud Messaging 发送推送通知
【发布时间】:2014-05-15 11:46:43
【问题描述】:

我已经在 cordova 3.4 中开发了 android 应用程序,现在我想将自动通知发送到他们的手机。为此,我参考以下网站

我阅读了以上所有链接,然后做了他们告诉的所有事情,比如在谷歌云消息传递上创建项目,获取项目 ID,创建服务器密钥(在文本框中我给出了我的私有服务器 IP)然后是 API 密钥,我还有注册 ID 设备我在 php 中编写了用于向我的设备发送推送通知的代码,但它给了我“unauthorised Error 404

我的代码如下

    define( 'API_ACCESS_KEY', 'my api key' );

    //$registrationIds = array( $_GET['id'] );
    $registrationIds = array($id);
    //var_dump($registrationIds);die;
    //prep the bundle
    $msg = array
    (
            'message'       => 'New Jobs is updated',
            'title'         => 'get the job in cad',
            'subtitle'      => 'get the job in cad',
            'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
            'vibrate'   => 1,
            'sound'     => 1
    );

    $fields = array
    (
            'registration_ids'  => $registrationIds,
            'data'              => $msg
    );

    $headers = array
    (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch );
    curl_close( $ch );

    echo $result;

我只是点击 url 并将设备的注册 ID 发送到我的 php 页面,但是这个页面给了我错误 unauthorised Error 401

Que.1.这可以从托管在私人服务器上的php发送推送通知吗?

Que.2 向安装应用程序的用户发送推送通知是否必须提供设备注册 ID?

请任何人帮助我如何解决上述错误,如果有人有其他解决方案,请告诉我。我从昨天开始尝试但没有达到我的目标,所以请帮助我。

【问题讨论】:

    标签: php android git cordova google-cloud-messaging


    【解决方案1】:

    这是 GCM 代码,虽然它在 CI 框架中,但它可以在我的设备上正常工作,但希望你能使用它。必须为推送添加 device_id 以及您将为该站点获得的密码。试试看吧。

    function sendNotification($device_id,$message) {
            /*echo $device_id;
            echo "<br>";
            echo $message;*/
    
            // note: you have to specify API key in config before
            $this->load->library('gcm');
            // simple adding message. You can also add message in the data,
            // but if you specified it with setMesage() already
            // then setMessage's messages will have bigger priority
            $msg = $this->gcm->setMessage($message);
            //var_dump($message);
    
            // add recepient or few
            $this->gcm->addRecepient($device_id);
            //var_dump($device_id);
            // set additional data
            //$this->gcm->setData($params);
            // also you can add time to live
            $this->gcm->setTtl(500);
            // and unset in further
            //$this->gcm->setTtl(false);
    
            // set group for messages if needed
            //$this->gcm->setGroup('Test');
            // or set to default
            //$this->gcm->setGroup(false);
            // send
            return $this->gcm->send();
        }
    

    希望它能解决你的问题。

    【讨论】:

    • 另外你必须加载 gcm 库,否则它将无法工作。
    【解决方案2】:
    <?php 
    $register_keys = array("YOUR_REGISTRY_KEYS")
    $google_api_key = "YOUR_API_KEY";
    $registatoin_ids = $register_keys; 
    $message = array("price" => $message);  
    // 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));
    }
    //print_r($result);
    // Close connection
    curl_close($ch);
    ?>
    

    【讨论】:

    • 如果你能简单解释/总结一下这段代码的作用会更好。
    • 不清楚这是如何假设 OP 提出的不同问题。
    猜你喜欢
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    相关资源
    最近更新 更多