Advertisement
Guest User

Code

a guest
Mar 7th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. $apiKey = "AIzaSyDVdlGuMlIisLhq0WDhU7JEYJ1AEef5KQg";
  6. $devices = array('3f614477c1cdb0fe');
  7. $message = "juhu";
  8.  
  9. $gcpm = new GCMPushMessage($apiKey);
  10. $gcpm->setDevices($devices);
  11. $response = $gcpm->send($message, array('title' => 'Test'));
  12.  
  13.  
  14. class GCMPushMessage {
  15.  
  16.     var $url = 'https://android.googleapis.com/gcm/send';
  17.     var $serverApiKey = "AIzaSyDVdlGuMlIisLhq0WDhU7JEYJ1AEef5KQg";
  18.     var $devices = array();
  19.    
  20.     /*
  21.         Constructor
  22.         @param $apiKeyIn the server API key
  23.     */
  24.     function GCMPushMessage($apiKeyIn){
  25.         $this->serverApiKey = $apiKeyIn;
  26.     }
  27.  
  28.     /*
  29.         Set the devices to send to
  30.         @param $deviceIds array of device tokens to send to
  31.     */
  32.     function setDevices($deviceIds){
  33.    
  34.         if(is_array($deviceIds)){
  35.             $this->devices = $deviceIds;
  36.         } else {
  37.             $this->devices = array($deviceIds);
  38.         }
  39.    
  40.     }
  41.  
  42.     /*
  43.         Send the message to the device
  44.         @param $message The message to send
  45.         @param $data Array of data to accompany the message
  46.     */
  47.     function send($message, $data = false){
  48.        
  49.         if(!is_array($this->devices) || count($this->devices) == 0){
  50.             $this->error("No devices set");
  51.         }
  52.        
  53.         if(strlen($this->serverApiKey) < 8){
  54.             $this->error("Server API Key not set");
  55.         }
  56.        
  57.         $fields = array(
  58.             'registration_ids'  => $this->devices,
  59.             'data'              => array( "message" => $message ),
  60.         );
  61.        
  62.         if(is_array($data)){
  63.             foreach ($data as $key => $value) {
  64.                 $fields['data'][$key] = $value;
  65.             }
  66.         }
  67.  
  68.         $headers = array(
  69.             'Authorization: key=' . $this->serverApiKey,
  70.             'Content-Type: application/json'
  71.         );
  72.  
  73.         // Open connection
  74.         $ch = curl_init();
  75.        
  76.         // Set the url, number of POST vars, POST data
  77.         curl_setopt( $ch, CURLOPT_URL, $this->url );
  78.        
  79.         curl_setopt( $ch, CURLOPT_POST, true );
  80.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
  81.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  82.        
  83.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
  84.        
  85.         // Avoids problem with https certificate
  86.         curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
  87.         curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
  88.        
  89.         // Execute post
  90.         $result = curl_exec($ch);
  91.        
  92.         // Close connection
  93.         curl_close($ch);
  94.        
  95.         return $result;
  96.     }
  97.    
  98.     function error($msg){
  99.         echo "Android send notification failed with error:";
  100.         echo "\t" . $msg;
  101.         exit(1);
  102.     }
  103. }
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement