<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>
<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>
在上面的代码中,我们向java脚本发送一个id并发送msg ..
因此希望将ID的列表发送到JAVASCRIPT函数并在javascript函数中打印这些值。
不,我不需要整个表格..
但我想使用一个表单,一个提交按钮&amp;一个文本字段发送到多个设备。
感谢提前......
这是我们接受消息的表单屏幕
<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="puski" method="post" action="send_message.php">
<input type="text" name="message"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
这是读取所有ID并将消息发送到GCM.php的文件
<?php if (isset($_POST["message"])) {
$message = $_POST["message"];
include_once './GCM.php';
$gcm = new GCM();
include_once 'db_functions.php';
$db = new DB_Functions();
$users = $db->getAllUsers();
while ($row = mysql_fetch_array($users)) {
$gcm_array[]=$row['gcm_regid'];
}
$message = array("price" => $message);
$result = $gcm->send_notification($gcm_array, $message);
echo $result;
}?&gt;
我们有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);
// 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;
}
}
?>
我们仍然收到错误
Fatal error: Call to undefined function curl_init() in C:\wamp\www\gcm_server_php\GCM.php on line 41
请帮忙
答案 0 :(得分:0)
步骤1:创建一个php页面,您可以在其中输入需要为所有用户发送的消息
message.php
html>
<head>
<link rel="stylesheet" type="text/css" href="design.css"/>
</head>
<body>
<div id="container">
<h6>GCM</h6>
<h3>MESSAGE</h3>
<form action="notification.php" method="post">
<textarea id="message" name="msg" rows="6" cols="41" autofocus="true"></textarea>
<br />
<br />
<input type="submit" class="btn_class" value="Send"/>
</form>
</div>
</body>
design.css
div#container{
position:absolute;
width:400px;
height:350px;
top:50%;
left:50%;
margin-top:-225px;
margin-left:-200px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.35);
}
#message{
margin-left:25px;
}
.btn_class{
color:blue;
margin-left:170px;
}
h6{
text-align:center;
font-family:sans-serif;
font-size:20px;
}
h3{
margin-left:30px;
font-family:sans-serif;
font-size:14px;
}
步骤2:创建notification.php文件,收到消息后,将使用您的api密钥和注册表将其发送给所有用户
notification.php
<?php
$message=$_POST["msg"];
........
// construct sql query for database access
while($row=mysql_fetch_array($result)){
$registrationIDs[] = $row["gcm_regid"]; // store all reg ids from the column "gcm_regid" in the array
}
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
?>