Introduction:
In this article we set up the PHP form for GCM, because we send a message through browser and receive it our own mobile device.
Design Simple Form in Bootstrap
Now there are three things which we need to design the form; first, we need the Message, API KEY and third is GCM ID and one button for send message.
Code
You can save the code by giving the file extension index.php and now our two things are completed and one is left which is set up this service in androi.d Now inthe next article we will implement in android studio and almost all of our code is completed and the code is simple and available on different websites. I will share the code for giving a easy way to learn GCM.
- <?php
-
- functionsendPushNotificationToGCM($registatoin_ids, $message,$ApiKey) {
-
- $url = 'https://android.googleapis.com/gcm/send';
-
- $fields = array(
- 'registration_ids' => $registatoin_ids,
- 'data' => $message,
- );
-
-
-
-
- $headers = array(
- 'Authorization: key=' . $ApiKey ,
- 'Content-Type: application/json'
- );
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
- $result = curl_exec($ch);
- if ($result === FALSE) {
- die('Curl failed: ' . curl_error($ch));
- }
- curl_close($ch);
- return $result;
- }
- ?>
- <?php
-
-
- $pushStatus = "";
- if($_POST && isset($_POST["message"])&&isset($_POST["apiKey"])&&isset($_POST["deviceId"])) {
- $pushMessage = $_POST["message"];
- $apiKey = $_POST["apiKey"];
- $gcmRegID = $_POST["deviceId"];
- if (isset($gcmRegID) &&isset($pushMessage)) {
- $ApiKey = $apiKey;
- $gcmRegIds = array($gcmRegID);
- $message = array("m" => $pushMessage);
- $pushStatus = sendPushNotificationToGCM($gcmRegIds, $message,$ApiKey);
- }
- }
-
-
- ?>
- <html>
-
- <head>
- <title>Google Cloud Messaging (GCM) Server </title>
- </head>
- <script src="//code.jquery.com/jquery-1.12.0.min.js">
- </script>
- <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js">
- </script>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
-
- <body>
- <center>
- <h1>Google Cloud Messaging (GCM) Server in PHP</h1> <br></br>
- <form method="post" action="" role="form">
- <div class="form-inline"> <label for="inputEmail" style="margin-right:5px;">Message</label> <textarea rows="2" name="message" cols="50" class="form-control" placeholder="Message"></textarea> </div> <br>
- <div class="form-inline"> <label for="inputEmail" style="margin-right:15px;">API Key</label> <textarea rows="2" name="apiKey" cols="50" class="form-control" placeholder="API Key"></textarea> </div> <br>
- <div class="form-inline"> <label for="inputEmail" style="margin-right:15px;">GCM ID</label> <textarea rows="2" name="deviceId" cols="50" class="form-control " placeholder="GCM ID"></textarea> </div> <br>
- <div class="btnbtn-info"><input type="submit" role="button" value="Send" /></div>
- </form>
- <p>
- <h3><?php echo $pushStatus; ?></h3></p>
- </center>
- </body>
-
- </html>
Read more articles on PHP: