<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>
在我們發送一個單一的ID,以Java腳本和發送味精上面的代碼..推送通知發送給ID的數組一次
所以想送將數組的ID一次性添加到JAVASCRIPT FUNCTION並在javascript函數中打印值。
沒有我不需要整個窗體..
,但我想用一種形式,一個提交按鈕&一個文本字段發送到多個設備。
在此先感謝...
這是一個接受消息的表單屏幕
<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;
}?>
我們有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
請幫
總之你想發佈整個表單嗎? – Andreas
不,我不需要整個表格。 但我想使用一種形式,一個提交按鈕和一個文本字段發送到多個設備。 – user2841396
還沒有得到答案仍然打開.. – user2841396