public static function getInstance() { if (!Gcm::$instance) { Gcm::$instance = new Gcm(); } return Gcm::$instance; }
public function checkMessage() { require './database/Database.php'; require './gcm/Gcm.php'; $db = new Database(); $dateTime = new DateTime('now', new DateTimeZone('Asia/Calcutta')); $queryTime = $dateTime->format('Y-m-d H:i:s'); $queryString = "SELECT * FROM `message` WHERE `sent` = 0 AND `datetime` <= '{$queryTime}' "; if ($query = $db->queryDb($queryString)) { $gcm = new Gcm(); $data = array(); foreach ($query as $row) { switch ($row['update_type']) { case 0: $data['type'] = 0; $data['message'] = $row['description']; break; case 1: $data['type'] = 1; $data['name'] = $row['name']; $data['location'] = $row['location']; $data['desc'] = $row['description']; $data['long'] = $row['longitude']; $data['lat'] = $row['latitude']; break; case 2: $data['type'] = 2; $data['url'] = 'http://10.42.0.1/gcm/images/' . $row['pick']; $data['desc'] = $row['description']; break; default: throw new Exception('Unsupported operation'); } if ($gcm_response = $gcm->sendNotifcation(json_encode($data))) { $jsonResponse = json_decode($gcm_response, TRUE); $message_id = $jsonResponse['message_id']; $this->setSent($db, $message_id, $row['message_id']); } sleep(1); } } }
<?php Route::any('matriphe/gcm', function () { $data = []; if (Request::has('device_id') && Request::has('gcm_id')) { $subject = Request::input('subject', 'Test Subject'); $message = Request::input('message', 'Tes Message'); $device_id = Request::input('device_id'); $gcm_id = Request::input('gcm_id'); $result = Gcm::push($device_id, $gcm_id, $subject, $message); $data['result'] = $result; } return view('gcm::form')->with($data); });
public function postComment() { if (Request::ajax()) { $id = Input::get('id'); $comentario = Input::get('comment'); if (strlen($comentario) < 4) { return 'El comentario es muy corto'; } $publication = Publicaciones::find($id); $comentarios = new Comentarios(); $comentarios->user_id = Auth::id(); $comentarios->pub_id = $id; $comentarios->comentario = $comentario; $comentarios->updated_at = date('Y-m-d', time()); $comentarios->created_at = date('Y-m-d', time()); $comentarios->save(); $msg = "Han comentado tu publicacion: " . $publication->titulo; $user = User::find($publication->user_id); $data = array('message' => $msg, 'title' => $msg, 'msgcnt' => null, 'timeToLive' => 3000); $gcm = GcmDevices::where('user_id', '=', $user->id)->orderBy('id', 'DESC')->get(array('gcm_regid')); $regId = array(); $i = 0; foreach ($gcm as $g) { $regId[$i] = $g['gcm_regid']; $i++; } $doGcm = new Gcm(); $response = $doGcm->send_notification($regId, $data); return Response::json(array('type' => 'success', 'msg' => 'Comentario Guardado Sactisfactoriamente', 'date' => date('d-m-Y', strtotime($comentarios->created_at)))); } }
<?php class Gcm { private $x0_seed; private $a_multiplier; private $c_additive_constant; private $m_module; function Gcm() { $this->x0_seed = 5; $this->a_multiplier = 5; $this->c_additive_constant = 0; $this->m_module = 10007; } private function value($aXi = 0) { return ($this->a_multiplier * $aXi + $this->c_additive_constant) % $this->m_module; } public function Xn() { $aXi = $this->value($this->x0_seed); echo $aXi . "\n"; while ($aXi != $this->x0_seed) { $aXi = $this->value($aXi); echo $aXi . "\n"; } } } $gcm = new Gcm(); $gcm->Xn();
return $db->queryInsertDb($insertQuery); } public static function updateRegisteredToken($token, $tokenId) { require_once './database/Database.php'; $db = new Database(); $updateQuery = "UPDATE `registered_devices` SET `token_id`='{$token}' WHERE `registration_id`='{$tokenId}'"; return $db->queryUpdateDb($updateQuery); } } if (isset($_GET['token']) && $_GET['token'] != '') { if ($_GET['refreshed'] == 'false') { if (Register::registerDevice($_GET['token'])) { //send notification to registered user require './gcm/Gcm.php'; $gcm = new Gcm(); $messageWellcome = '{"type": "0", "message":"Well come to Archismat. We are happy to see you."}'; $gcm->sendNotifcation($messageWellcome, $_GET['token']); //Notification sent echo mysqli_insert_id(Database::$conn); } else { echo -1; } } else { if ($_GET['refreshed'] == 'true') { if (Register::updateRegisteredToken($_GET['token'], $_GET['update_id'])) { echo 1; } else { echo -1; } }