使用GCM、PHP和MySQL的Android推送通知
我对推送通知有问题。我正在按照AndroidHive中提到的说明,使用Google云消息(GCM)、PHP和MySQL尝试Android推送通知。我的设备已向GCM注册,可以在数据库中查看记录,但在发送推送通知时,无法在设备上接收。我张贴的链接,我已经遵循。如果我遗漏了什么,请告诉我 清单文件:使用GCM、PHP和MySQL的Android推送通知,android,push-notification,Android,Push Notification,我对推送通知有问题。我正在按照AndroidHive中提到的说明,使用Google云消息(GCM)、PHP和MySQL尝试Android推送通知。我的设备已向GCM注册,可以在数据库中查看记录,但在发送推送通知时,无法在设备上接收。我张贴的链接,我已经遵循。如果我遗漏了什么,请告诉我 清单文件: AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://s
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.pushnotifications"
android:versionCode="1"
android:versionName="1.0" >
<!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Main activity. -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- Register Activity -->
<activity
android:name=".RegisterActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main Activity -->
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.androidhive.pushnotifications" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
index.php
index.php
<!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(){
});
function sendPushNotification(id){
var data = $('form#'+id).serialize();
$('form#'+id).unbind('submit');
$.ajax({
url: "send_message.php",
type: 'GET',
data: data,
beforeSend: function() {
},
success: function(data, textStatus, xhr) {
$('.txt_message').val("");
},
error: function(xhr, textStatus, errorThrown) {
}
});
return false;
}
</script>
<style type="text/css">
.container{
width: 950px;
margin: 0 auto;
padding: 0;
}
h1{
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 24px;
color: #777;
}
div.clear{
clear: both;
}
ul.devices{
margin: 0;
padding: 0;
}
ul.devices li{
float: left;
list-style: none;
border: 1px solid #dedede;
padding: 10px;
margin: 0 15px 25px 0;
border-radius: 3px;
-webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
-moz-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
color: #555;
}
ul.devices li label, ul.devices li span{
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12px;
font-style: normal;
font-variant: normal;
font-weight: bold;
color: #393939;
display: block;
float: left;
}
ul.devices li label{
height: 25px;
width: 50px;
}
ul.devices li textarea{
float: left;
resize: none;
}
ul.devices li .send_btn{
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0096FF), to(#005DFF));
background: -webkit-linear-gradient(0% 0%, 0% 100%, from(#0096FF), to(#005DFF));
background: -moz-linear-gradient(center top, #0096FF, #005DFF);
background: linear-gradient(#0096FF, #005DFF);
text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
border-radius: 3px;
color: #fff;
}
</style>
</head>
<body>
<?php
include_once 'db_functions.php';
$db = new DB_Functions();
$users = $db->getAllUsers();
if ($users != false)
$no_of_users = mysql_num_rows($users);
else
$no_of_users = 0;
?>
<div class="container">
<h1>No of Devices Registered: <?php echo $no_of_users; ?></h1>
<hr/>
<ul class="devices">
<?php
if ($no_of_users > 0) {
?>
<?php
while ($row = mysql_fetch_array($users)) {
?>
<li>
<form id="<?php echo $row["id"] ?>" name="" method="post" onsubmit="return sendPushNotification('<?php echo $row["id"] ?>')">
<label>Name: </label> <span><?php echo $row["name"] ?></span>
<div class="clear"></div>
<label>Email:</label> <span><?php echo $row["email"] ?></span>
<div class="clear"></div>
<div class="send_container">
<textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"></textarea>
<input type="hidden" name="regId" value="<?php echo $row["gcm_regid"] ?>"/>
<input type="submit" class="send_btn" value="Send" onclick=""/>
</div>
</form>
</li>
<?php }
} else { ?>
<li>
No Users Registered Yet!
</li>
<?php } ?>
</ul>
</div>
</body>
</html>
AndroidManifest.xml
index.php
index.php
$(文档).ready(函数(){
});
函数sendPushNotification(id){
var data=$('form#'+id).serialize();
$('form#'+id).unbind('submit');
$.ajax({
url:“send_message.php”,
键入:“GET”,
数据:数据,
beforeSend:function(){
},
成功:函数(数据、文本状态、xhr){
$('.txt_message').val(“”);
},
错误:函数(xhr、textStatus、errorshown){
}
});
返回false;
}
.集装箱{
宽度:950px;
保证金:0自动;
填充:0;
}
h1{
字体系列:“Helvetica Neue”,Helvetica,Arial,无衬线;
字体大小:24px;
颜色:#777;
}
分区清除{
明确:两者皆有;
}
保险设备{
保证金:0;
填充:0;
}
ul.li{
浮动:左;
列表样式:无;
边框:1px实心#dedede;
填充:10px;
边际:0 15px 25px 0;
边界半径:3px;
-webkit盒阴影:0 1px 5px rgba(0,0,0,0.35);
-moz盒阴影:0 1px 5px rgba(0,0,0,0.35);
盒影:0 1px 5px rgba(0,0,0,0.35);
字体系列:“Helvetica Neue”,Helvetica,Arial,无衬线;
颜色:#555;
}
ul设备li标签,ul设备li跨度{
字体系列:“Helvetica Neue”,Helvetica,Arial,无衬线;
字体大小:12px;
字体风格:普通;
字体变体:正常;
字体大小:粗体;
颜色:#393939;
显示:块;
浮动:左;
}
ul.li标签{
高度:25px;
宽度:50px;
}
ul.li文本区域{
浮动:左;
调整大小:无;
}
ul.devices li.send\u btn{
背景:-webkit梯度(线性,0%0%,0%100%,从(#0096FF)到(#005DFF));
背景:-webkit线性梯度(0%0%,0%100%,从(#0096FF)到(#005DFF));
背景:-莫兹线性梯度(中上,#0096FF,#005DFF);
背景:线性梯度(#0096FF,#005DFF);
文本阴影:0 1px 0 rgba(0,0,0,0.3);
边界半径:3px;
颜色:#fff;
}
已注册的设备数量:
-
当我试图使用上面的链接实现推送通知时,我也遇到了同样的问题。
经过几天无意义的研发,我找到了解决方案,在php代码中提供了错误的google键。我收到一些404状态错误
那么解决办法就是
- 登录到谷歌控制台帐户
- 创建新项目
- 在API&Auth->选择API并启用Android云消息服务
- 然后在API&Auth->公共API访问部分中选择“凭证”->“创建新密钥”
- 在“创建新密钥”对话框下选择“浏览器密钥”。然后再次单击“创建”(不在textarea中添加任何内容)
- 它将获得新的API密钥李>
复制此api密钥并将其粘贴到register.php代码中
$headers = array(
'Authorization: key=' . 'AIzaSyDa4aTJMblZixlatAZjdDXxCf_2mJqc3WM', // HERE IT IS DEMO KEY
'Content-Type: application/json'
);
现在您可以尝试发送通知。它应该会起作用
享受。。。快乐的编码。这是一个工作脚本
<?php
class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
static function send_notification($data=array()) {
// Set POST variables
$ = 'https://android.googleapis.com/gcm/send';
$apiKey = 'API key register via google console';
$data = array(
'registration_ids' => array($data['device_token']),
'data' =>array("message"=>$data['message'])
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the , number of POST vars, POST data
curl_setopt($ch, CURLOPT_, $);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
//echo json_encode($data);
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
print_r($result);
// Close connection
curl_close($ch);
}
}
您还可以使用在线工具测试GCM推送通知。使用这些在线工具,您将知道您的代码中是否存在问题或使用了错误的凭据(GCM密钥)
除此之外,您还可以使用chiarg的脚本进行测试,该脚本将显示状态(失败和成功以及其他详细信息)
希望这会对您有所帮助。如果您需要帮助,您必须发布更多详细信息。您是否从谷歌的GCM服务器获得了成功的响应?此外,请向我们展示您的清单和相关客户代码。发布到教程的链接没有帮助。因为代码非常大,我只能发布链接。是的,我收到服务器的响应,说设备已注册。我将用代码和清单文件编辑问题。谢谢发布清单。看起来不错。我的意思是,当我询问回复时,当您从服务器向GCM发送消息时,您会得到什么样的响应(我没有询问注册的问题)。不幸的是。我没有得到任何回应。我将发布我正在使用的index.php来
<?php
class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
static function send_notification($data=array()) {
// Set POST variables
$ = 'https://android.googleapis.com/gcm/send';
$apiKey = 'API key register via google console';
$data = array(
'registration_ids' => array($data['device_token']),
'data' =>array("message"=>$data['message'])
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the , number of POST vars, POST data
curl_setopt($ch, CURLOPT_, $);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
//echo json_encode($data);
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
print_r($result);
// Close connection
curl_close($ch);
}
}