Set Up PHP Form For Cloud Messaging Service In Google: Part Two

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.

design

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.

  1. <?php  
  2. //generic php function to send GCM push notification  
  3. functionsendPushNotificationToGCM($registatoin_ids, $message,$ApiKey) {  
  4. //Google cloud messaging GCM-API url  
  5. $url = 'https://android.googleapis.com/gcm/send';  
  6.   
  7. $fields = array(  
  8. 'registration_ids' => $registatoin_ids,  
  9. 'data' => $message,  
  10. );  
  11.   
  12.   
  13. // Google Cloud Messaging GCM API Key  
  14.   
  15. $headers = array(  
  16. 'Authorization: key=' . $ApiKey ,  
  17. 'Content-Type: application/json'  
  18. );  
  19. $ch = curl_init();  
  20. curl_setopt($ch, CURLOPT_URL, $url);  
  21. curl_setopt($ch, CURLOPT_POST, true);  
  22. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  24. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
  25. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  26. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));  
  27. $result = curl_exec($ch);   
  28. if ($result === FALSE) {  
  29. die('Curl failed: ' . curl_error($ch));  
  30. }  
  31. curl_close($ch);  
  32. return $result;  
  33. }  
  34. ?>  
  35.     <?php  
  36.   
  37. //this block is to post message to GCM on-click  
  38. $pushStatus = "";  
  39. if($_POST && isset($_POST["message"])&&isset($_POST["apiKey"])&&isset($_POST["deviceId"])) {  
  40. $pushMessage = $_POST["message"];  
  41. $apiKey = $_POST["apiKey"];  
  42. $gcmRegID = $_POST["deviceId"];  
  43. if (isset($gcmRegID) &&isset($pushMessage)) {   
  44. $ApiKey = $apiKey;  
  45. $gcmRegIds = array($gcmRegID);  
  46. $message = array("m" => $pushMessage);  
  47. $pushStatus = sendPushNotificationToGCM($gcmRegIds, $message,$ApiKey);  
  48. }   
  49. }  
  50.   
  51.   
  52. ?>  
  53.         <html>  
  54.   
  55.         <head>  
  56.             <title>Google Cloud Messaging (GCM) Server </title>  
  57.         </head>  
  58.         <script src="//code.jquery.com/jquery-1.12.0.min.js">  
  59.             </script>  
  60.             <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js">  
  61.                 </script>  
  62.                 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  
  63.                 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  64.                 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
  65.   
  66.                 <body>  
  67.                     <center>  
  68.                         <h1>Google Cloud Messaging (GCM) Server in PHP</h1> <br></br>  
  69.                         <form method="post" action="" role="form">  
  70.                             <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>  
  71.                             <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>  
  72.                             <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>  
  73.                             <div class="btnbtn-info"><input type="submit" role="button" value="Send" /></div>  
  74.                         </form>  
  75.                         <p>  
  76.                             <h3><?php echo $pushStatus; ?></h3></p>  
  77.                     </center>  
  78.                 </body>  
  79.   
  80.         </html> 

Read more articles on PHP:

Up Next
    Ebook Download
    View all
    Learn
    View all