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>

in the above code we are sending one single id to java script and sending the msg ..

SO WANT TO SEND THE ARRAY OF ID'S AT ONCE TO JAVASCRIPT FUNCTION AND print the values in javascript function.

no i dont need the whole form ..

but I want to use one form, one submit button & one text field to send to multiple devices.

THANKS IN ADVANCE ...

This is our Form screen that accepts the message

<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>

This is the file that reads all the ids and send the message to 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; 

} ?>

We have 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;
}

}

?>

Still we get the error

Fatal error: Call to undefined function curl_init() in C:\wamp\www\gcm_server_php\GCM.php on line 41

Please help

Boann
  • 48,794
  • 16
  • 117
  • 146

1 Answers1

0

Step 1: Create a php page where you can input the message that needs to be sent for all the users

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

Step 2: create notification.php file that upon receiving the message will send it to all the users using your api key and 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 ),
             );
?>
Harshad Ranganathan
  • 1,820
  • 16
  • 31