2013-05-09 82 views
3

我是GCM/Java的新手,我在php中经验丰富,但只有mysql。使用此代码发送推送通知给多个设备(PHP)

我发现这个教程: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

这是非常有用的,我得到它的工作。我唯一的问题是,这个代码是一次发送到一个设备。 我试着编辑它,所以它发送到多个设备使用一个表格&一个提交按钮,但没有成功。

此代码从Java(客户端)信息获取并将其放入使用php(服务器)的mysql数据库中。 它还将设备的GCM注册ID注册到mysql数据库 中,并且您可以使用id的GCM注册ID通过id向设备发送通知。

在这里你在表格中的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["klas"] ?></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> 

它使用AJAX将其发送到GCM:

Send_message.php:

<?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; 
} 
?> 

正如你看,它得到everthing ID, 但我想用一个表格,一个提交按钮&一个文本字段发送到多个设备。

如果有人能指导我如何做或给我一些例子,我会非常感激。

回答

1

我不知道PHP,但GCM.php从您提供的链接似乎已经支持发送推送通知到多个设备:

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, 
    ); 

您应该简单地传递给send_notification功能包含多个注册一个数组ID,它会向所有人发送通知。

+0

Thnx,但我确实已经解决了它^^,下次我发布自己的答案,以帮助其他人。我现在没有具体的代码,我稍后更新。 – iLuvCode 2013-05-18 20:44:55

+0

什么是代码,我坚持完全一样的东西。 – Jerome 2014-07-11 23:18:27

相关问题