Php 使用GCM for my android应用程序向所有设备发送推送通知
我正在制作一个android应用程序。我想为我的android应用程序实现推送通知。我在线查看了androidhive.com上的一个不错的教程。我想向所有安装了该应用的设备发送一条消息Php 使用GCM for my android应用程序向所有设备发送推送通知,php,android,mysql,push-notification,Php,Android,Mysql,Push Notification,我正在制作一个android应用程序。我想为我的android应用程序实现推送通知。我在线查看了androidhive.com上的一个不错的教程。我想向所有安装了该应用的设备发送一条消息 <?php if (isset($_GET["regId"]) && isset($_GET["message"])) { $regId = $_GET["regId"]; $message = $_GET["message"]; include_once './
<?php
if (isset($_GET["regId"]) && isset($_GET["message"])) {
$regId = $_GET["regId"];
$message = $_GET["message"];
include_once './GCM.php';
$gcm = new GCM();
$registatoin_ids = array($regId);
$message = array("price" => $message);
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
}
?>
下面的代码创建表单以向每个regid发送推送通知。我想向所有设备发送推送通知
<!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>
$(文档).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;
}
已注册的设备数量:
-
循环查看您的注册ID。如果您有超过1000个注册用户,请每1000个注册用户发送一条消息
GCM中没有发送到“所有设备”的选项。那么,问题出在哪里?您是否为要向其发送通知的android设备生成了注册id?您可以使用谷歌主题