thieumao

push gcm demo

Sep 5th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.84 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Android GCM</title>
  6. </head>
  7.  
  8. <?php
  9. function getVar($varName) {
  10.     static $countVar = array();
  11.     $result = null;
  12.     if (isset($_REQUEST[$varName])) {
  13.         $result = $_REQUEST[$varName];
  14.     }
  15.     if (empty($result) && isset($_SESSION[$varName])) {
  16.         $result = $_SESSION[$varName];
  17.     }
  18.     if (!empty($result)) {
  19.         $_SESSION[$varName] = $result;
  20.     }
  21.     if (!is_array($result)) {
  22.         return $result;
  23.     } else {
  24.         if (!isset($countVar[$varName])) {
  25.             $count = 0;
  26.         } else {
  27.             $count = $countVar[$varName] + 1;
  28.         }
  29.         if ($count == sizeof($result)) {
  30.             $count = 0;
  31.         }
  32.         $countVar[$varName] = $count;
  33.        
  34.         return $result[$count];
  35.     }
  36.    
  37. }
  38.  
  39. $page_size = 10000;
  40. session_start();
  41. if ($_GET['auto'] == 1) {
  42.     $page = getVar('page');
  43. } else {
  44.     $page = 0;
  45. }
  46. if (empty($page)) {
  47.     $page = 0;
  48. }
  49.  
  50. if ($_REQUEST['auto'] == '1') {
  51.     require_once dirname(__FILE__) . '/www/delivery/common.php';
  52.     MAX_Dal_Delivery_Include();
  53.    
  54.     $aConf = $GLOBALS ['_MAX'] ['CONF'];
  55.     $from = $page * $page_size * 5;
  56.     $query = "  SELECT d1.deviceid, d1.gcmid, d1.lastupdate, d1.packagename FROM `ox_devide_info` d1
  57.                 WHERE NOT EXISTS (SELECT d2.id FROM `ox_devide_info` d2 WHERE d1.deviceid = d2.deviceid AND d1.id <> d2.id AND d1.lastupdate <= d2.lastupdate)
  58.                 AND d1.packagename='com.eway.xjoy'
  59.                 ORDER BY d1.lastupdate DESC LIMIT $from, " . ($page_size * 5);
  60.     $registrationIDs = array();
  61.     $res = OA_Dal_Delivery_query ( $query );
  62.     if (is_resource ( $res )) {
  63.         while ( $row = OA_Dal_Delivery_fetchAssoc ( $res ) ) {
  64.             $registrationIDs[] = $row['gcmid'];
  65.         }
  66.     }
  67.  
  68.     if (sizeof($registrationIDs) > 0) {
  69.        
  70.     }
  71.    
  72.     $title = trim(stripslashes(getVar('title')));
  73.     $url = trim(stripslashes(getVar('url')));
  74.     $sizeapk = trim(stripslashes(getVar('sizeapk')));
  75.     $code = trim(stripslashes(getVar('code')));
  76.    
  77.     $serverApiKey = "dien vao day";
  78.    
  79.         $registrationIDTemps = array();
  80.  
  81.         for ($j= 0; $j < $page_size; $j++ ) {
  82.             if ($j * 5 < sizeof($registrationIDs)) {
  83.                 $registrationIDTemps[] = $registrationIDs[$j * 5];
  84.             }
  85.         }
  86.         //$registrationIDTemps[] = 'APA91bE_CWrSZS1IL3hyTMKLAixUs9cG9ZqZdsxIocsnwXSRaWQmcP8vn2K42xYjxv-rXrUyY5s24PlXh7MTlFiwF-d_uGt0sJ4IOK2NENPpfyEhSLWe88kMm_762o8oZ2gBJFYt2r7F';
  87.          $fields = array(
  88.             'registration_ids' => $registrationIDTemps
  89.             , 'data' => array(
  90.                 'type' => 'New'
  91.                 , 'title' => 'GCM'
  92.                 , 'versionCode' => $code
  93.                 , 'apk' => $url
  94.                 , 'productName' => $title
  95.                 , 'productSize' => $sizeapk
  96.             )
  97.          );
  98.  
  99.         $url = 'https://android.googleapis.com/gcm/send';
  100.        
  101.          $headers = array(
  102.             'Content-Type:application/json',
  103.             'Authorization:key=' . $serverApiKey
  104.          );
  105.        
  106.    
  107.         $sent = intval($_SESSION['sent']);
  108.         $sent += count($registrationIDTemps);
  109.         $_SESSION['sent'] = $sent;
  110.        
  111.         // Open connection
  112.         $ch = curl_init();
  113.        
  114.         // Set the url, number of POST vars, POST data
  115.         curl_setopt( $ch, CURLOPT_URL, $url );
  116.         curl_setopt( $ch, CURLOPT_POST, true );
  117.         curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
  118.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  119.         curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
  120.         curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
  121.        
  122.         // Execute post
  123.         $result = curl_exec($ch);
  124.  
  125.         curl_close($ch);
  126.  
  127.         try {
  128.             $result = json_decode($result);
  129.             $success = $result->success;
  130.            
  131.             $sentSuccess = intval($_SESSION['sentSuccess']);
  132.             $sentSuccess += intval($success);
  133.             $_SESSION['sentSuccess'] = $sentSuccess;
  134.         } catch (Exception $e) {
  135.            
  136.         }
  137.  
  138. } else {
  139.     $_SESSION['sent'] = 0;
  140.     $_SESSION['sentSuccess'] = 0;
  141. }
  142.  
  143.  
  144. if ($_GET['auto'] == 1) {
  145.     $page = $page + 1;
  146. }
  147. if ($_GET['auto'] == 1) {
  148.     echo '<meta http-equiv="refresh" content="2">';
  149. }
  150. $_SESSION['page'] = $page;
  151. echo "SEND: " . intval($_SESSION['sent']) . "<br />\n";
  152. echo "SEND SUCCESS: " . intval($_SESSION['sentSuccess']) . "<br />\n";
  153. ?>
  154. <form method="POST" action="<?php echo $_SERVER['REQUEST_URI'];?>?auto=1" id="push_message">
  155.     <table width=50% cellpadding = 5 cellspacing=0 border=0>
  156.         <tr>
  157.             <td>Title</td>
  158.             <td><input type="text" name="title" value="<?php echo getVar('title')?>" size="60" /></td>
  159.         </tr>
  160.         <tr>
  161.             <td>url</td>
  162.             <td><input type="text" name="url" value="<?php echo getVar('url')?>" size="60"></td>
  163.         </tr>
  164.         <tr>
  165.             <td>Filesize</td>
  166.             <td><input type="text" name="sizeapk" value="<?php echo getVar('sizeapk')?>" size="60"></td>
  167.         </tr>  
  168.         <tr>
  169.             <td>Version code</td>
  170.             <td><input type="text" name="code" value="<?php echo getVar('code')?>" size="60"></td>
  171.         </tr>          
  172.         <tr>
  173.             <td colspan="2">&nbsp;</td>
  174.         </tr>
  175.         <tr>
  176.             <td>&nbsp;</td>
  177.             <td>
  178.                 <button>Send notification</button>
  179.             </td>
  180.     </table>   
  181. </form>
Advertisement
Add Comment
Please, Sign In to add comment