2014-02-22 71 views
0
<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 

請幫

+0

總之你想發佈整個表單嗎? – Andreas

+0

不,我不需要整個表格。 但我想使用一種形式,一個提交按鈕和一個文本字段發送到多個設備。 – user2841396

+0

還沒有得到答案仍然打開.. – user2841396

回答

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密鑰將其發送給所有的用戶和reg IDS

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), 
      ); 
?> 
+0

嗨,我的名字是卡羅爾。感謝HAR。我用完整的代碼更新了這個問題。仍然沒有得到答案。 – user2841396

+0

你有沒有在你的php.ini文件中包含下面的擴展行「extension = php_curl.dll」 –

+0

嘿,檢查一下,http://stackoverflow.com/questions/12076641/call-to-undefined-function-curl-init-帶捲曲延伸可能存在問題。 –