簡體   English   中英

使用PHP獲取GCM的無效注冊錯誤

[英]Getting Invalid registration error for GCM using PHP

我正在嘗試使用php為Android創建推送通知。 但是我停留在某個地方,為此我使用了一個例子。 這是我的PHP代碼

網頁配置

<?php
/**
 * Database config variables
 */
define("DB_HOST", "localhost");
define("DB_USER", "");
define("DB_PASSWORD", "");
define("DB_DATABASE", "");

/*
 * Google API Key
 */
define("GOOGLE_API_KEY", "AIzaSyD_PC6UaeTbnSemX0hpY0eyawlE3EufRcA"); // Place your Google API Key
?>

我的GCM.php文件是這個

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GCM
 *
 * @author Ravi Tamada
 */
class GCM {

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

    }

    /**
     * Sending Push Notification
     */
    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);
        curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        // 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;
    }

}

?>

我的HTML是這個

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
            });
        </script>
    </head>
    <body>
        <form name="" method="post" action="register.php">
            <input type="text" name="name"/>
            <input type="text" name="email"/>
            <input type="text" name="regId"/>
            <input type="submit"/>
        </form>
    </body>
</html>

這是我正在使用的代碼,但每次都會向我顯示以下消息:{“ multicast_id”:4677806556758130005,“ success”:0,“ failure”:1,“ canonical_ids”:0,“ results”:[{“ error”:“ InvalidRegistration“}]}

我不知道我在做什么錯

我對必須通過的GCM注冊ID感到困惑。 誰能告訴我這是什么以及在哪里找到它

我為此嘗試了我的項目ID和項目編號,但是卻一次又一次地收到此錯誤

任何幫助表示贊賞

謝謝

請傳遞您的gcm帳戶的服務器密鑰 (如果未生成),請生成它並通過它,它將100%有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM