Advertisement
Guest User

sending-push-notifications.php

a guest
Feb 27th, 2017
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. <?php
  2.     //Now, we have to send the temperature alert to registered users.
  3.     //generic php function to send GCM push notification
  4.    
  5.    function sendPushNotificationToGCM($registration_ids, $message) {
  6.         //Google cloud messaging GCM-API url
  7.         $url = 'https://android.googleapis.com/gcm/send';
  8.         $fields = array(
  9.             'registration_ids' => $registration_ids,
  10.             'data' => $message,
  11.         );
  12.         // Google Cloud Messaging GCM API Key
  13.         define("API_KEY", "AIzaSyAURFJi4C_SMLxCiJvTcfKdLfnH0enA-WE");   // edit to add your API Key
  14.         $headers = array(
  15.             'Authorization: key=' . API_KEY,
  16.             'Content-Type: application/json'
  17.         );
  18.         $ch = curl_init();
  19.         curl_setopt($ch, CURLOPT_URL, $url);
  20.         curl_setopt($ch, CURLOPT_POST, true);
  21.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  22.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  23.         curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
  24.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  25.         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  26.         $result = curl_exec($ch);              
  27.         if ($result === FALSE) {
  28.             die('Curl failed: ' . curl_error($ch));
  29.         }
  30.         curl_close($ch);
  31.         return $result;
  32.     }
  33. ?>
  34.  
  35. <?php
  36.    
  37.     //this block is to receive the GCM regId from users and save them in the same MySQL database in the table 'regids'
  38.     if(!empty($_GET["shareRegId"])) {
  39.         $gcmRegID  = $_POST["regId"]; //App sends the GCM Reg ID through post, make sure you are on the same network for this.
  40.         $server = 'localhost';
  41.         $username = 'root';
  42.         $password = 'raspberry';
  43.         $database = 'templog';
  44.  
  45.         $conn = mysql_connect($server, $username, $password);
  46.         $db = mysql_select_db($database);
  47.  
  48.         $command = "SELECT * FROM `regids` WHERE (`RegID` = '$gcmRegID');";
  49.         $a = mysql_query($command);
  50.         $r = mysql_num_rows($a);
  51.         $s = "DELETE FROM `regids` WHERE (`RegID` = '');";
  52.         if ($r) {
  53.             echo "gcm-regID available in database"; // to prevent multiple registrations
  54.         }
  55.         else {
  56.             $query = "INSERT INTO `regids` (`RegID`) VALUES ('$gcmRegID');";
  57.             mysql_query($query);
  58.             echo "done!";
  59.         }
  60.         mysql_query($s);
  61.         mysql_close();
  62.         exit;
  63.     }
  64.    
  65.     //Code to get GCM RegIDs from the database and sending the alert
  66.     $pushStatus = "";  
  67.     if(!empty($_GET["push"])) {
  68.         $server = 'localhost';
  69.         $username = 'root';
  70.         $password = 'raspberry';
  71.         $database = 'templog';
  72.         //Connect to the database
  73.         $conn = mysql_connect($server, $username, $password);
  74.         $db = mysql_select_db($database);
  75.        
  76.         $query = "SELECT * FROM `regids`;";
  77.         $result = mysql_query($query);
  78.         $n = mysql_numrows($result);
  79.         mysql_close();
  80.         $RegistrationIDs = array();
  81.         $i = 0;
  82.         while ($i < $n) {
  83.             $RegistrationIDs[$i] = mysql_result($result,$i,"RegID");
  84.             $i++;
  85.         }
  86.         $pushMessage = $_GET["message"];   
  87.         $message = array("m" => $pushMessage); 
  88.         $pushStatus = sendPushNotificationToGCM($RegistrationIDs, $message);
  89.        
  90.         #This is just to help when you test your code to see if it is running properly
  91.         echo $pushMessage;
  92.         echo $pushStatus;
  93.         echo json_encode($RegistrationIDs);
  94.     }      
  95.    
  96.    
  97.            
  98. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement