无法在GCM中的演示服务器上注册设备

时间:2014-09-04 14:00:39

标签: php android

我正在尝试Android中的推送通知,我使用GCM api这样我在手机中收到错误当我尝试注册服务时#34;无法在演示服务器上注册设备"我怎么解决这个问题? 我的服务网址为"http://192.168.2.71:8080/gcm_server_files/register.php";

public interface Config {
    // CONSTANTS
    static final String YOUR_SERVER_URL =  "http://192.168.2.71:8080/gcm_server_files/register.php";
    // YOUR_SERVER_URL : Server url where you have placed your server files
    // Google project id
    static final String GOOGLE_SENDER_ID = "788729020569";  // Place here your Google project id

    /**
     * Tag used on log messages.
     */
    static final String TAG = "GCM Android Example";

    static final String DISPLAY_MESSAGE_ACTION =
            "com.androidexample.gcm.DISPLAY_MESSAGE";

    static final String EXTRA_MESSAGE = "message";


}

这个register.php文件我得到错误 注意:未定义的索引:第7行的C:\ xampp \ htdocs \ register.php中的名称

注意:未定义的索引:第8行的C:\ xampp \ htdocs \ register.php中的电子邮件

注意:未定义的索引:第9行的C:\ xampp \ htdocs \ register.php中的regId

<?php
require_once('loader.php');

// return json response 
$json = array();

$nameUser  = $_POST["name"];
$nameEmail = $_POST["email"];
$gcmRegID  = $_POST["regId"]; // GCM Registration ID got from device

/**
 * Registering a user device in database
 * Store reg id in users table
 */
if (isset($nameUser) && isset($nameEmail) && isset($gcmRegID)) {

    // Store user details in db
    $res = storeUser($nameUser, $nameEmail, $gcmRegID);

    $registatoin_ids = array($gcmRegID);
    $message = array("product" => "shirt");

    $result = send_push_notification($registatoin_ids, $message);

    echo $result;
} else {
    // user details not found
}
?>

从服务推送脚本

<?php
   require_once('loader.php');

    $resultUsers =  getAllUsers();
    if ($resultUsers != false)
        $NumOfUsers = mysql_num_rows($resultUsers);
    else
        $NumOfUsers = 0;
?>
<!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_push_notification_message.php",
                    type: 'GET',
                    data: data,
                    beforeSend: function() {

                    },
                    success: function(data, textStatus, xhr) {
                          $('.push_message').val("");
                    },
                    error: function(xhr, textStatus, errorThrown) {

                    }
                });
                return false;
            }
        </script>
        <style type="text/css">

            h1{
                font-family:Helvetica, Arial, sans-serif;
                font-size: 24px;
                color: #777;
            }
            div.clear{
                clear: both;
            }

            textarea{
                float: left;
                resize: none;
            }

        </style>
    </head>
    <body>


        <table  width="910" cellpadding="1" cellspacing="1" style="padding-left:10px;">
         <tr>
           <td align="left">
              <h1>No of Devices Registered: <?php echo $NumOfUsers; ?></h1>
              <hr/>
           </td>
          </tr> 
          <tr>
            <td align="center">
              <table width="100%" cellpadding="1" cellspacing="1" style="border:1px solid #CCC;" bgcolor="#f4f4f4">
                <tr>

               <?php
                if ($NumOfUsers > 0) {
                    $i=1;
                    while ($rowUsers = mysql_fetch_array($resultUsers)) {
                        if($i%3==0)
                          print "</tr><tr><td colspan='2'>&nbsp;</td></tr><tr>";
                 ?>
                        <td align="left">
                             <form id="<?php echo $rowUsers["id"] ?>" name="" method="post" onSubmit="return sendPushNotification('<?php echo $rowUsers["id"] ?>')">
                                <label><b>Name:</b> </label> <span><?php echo $rowUsers["name"] ?></span>
                                <div class="clear"></div>
                                <label><b>Email:</b></label> <span><?php echo $rowUsers["email"] ?></span>
                                <div class="clear"></div>
                                <div class="send_container">                                
                                    <textarea rows="3" name="message" cols="25" class="push_message" placeholder="Type push message here"></textarea>
                                    <input type="hidden" name="regId" value="<?php echo $rowUsers["gcm_regid"] ?>"/>
                                    <input type="submit"  value="Send Push Notification" onClick=""/>
                                </div>
                            </form>
                         </td>
                    <?php }
                } else { ?> 
                      <td>
                        User not exist.
                       </td>
                <?php } ?>

                </tr>
                </table>
            </td>
          </tr>  
        </table>


    </body>
</html>

1 个答案:

答案 0 :(得分:0)

将ajax请求类型切换为POST

$.ajax({
   url: "send_push_notification_message.php",
   type: 'POST',
   data: data,
   ...

您的表单甚至没有正确的标签:

<input type="text" name="name" value="<?php echo $rowUsers["name"] ?>"  /> 
<input type="text" name="email" value="<?php echo $rowUsers["email"] ?>"  />