我正在尝试使用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>
这是我正在使用的代码,但每次它向我显示消息 {&#34; multicast_id&#34;:4677806556758130005,&#34;成功&#34;:0,&#34;失效&#34;:1,&#34; canonical_ids&#34;:0,&#34;结果& #34;:[{&#34;错误&#34;:&#34; InvalidRegistration&#34;}]}
我不知道我做错了什么
我对GCM注册ID感到困惑,我必须通过。任何人都可以告诉我这是什么以及我找到它的地方
我尝试了我的项目ID和项目编号但是我一次又一次地得到了这个错误
感谢任何帮助
谢谢
答案 0 :(得分:0)
如果没有生成,请通过您的gcm帐户的服务器密钥,请生成并通过它100%