Ejemplo n.º 1
0
 function subs_notification_get()
 {
     $this->load->helper('push');
     $timestamp = strtotime("now");
     $uniquid = uniqid($timestamp);
     $this->db->select('a.id as uid,d.id,b.device_unique_id,b.platform as device_type,d.name as channel_name,e.date,e.show_time,e.end_date,e.end_time');
     $this->db->from('customers a');
     $this->db->join('customer_device b', 'a.id = b.user_id');
     $this->db->join('subscribe_event c', 'a.id = c.user_id');
     $this->db->join('channels d', 'c.channel_id = d.id');
     $this->db->join('livechannel_epg e', 'e.channel_id=d.id', 'left');
     $this->db->where("TIMESTAMPDIFF(MINUTE,NOW(),CONCAT_WS(' ',e.date,e.show_time)) <=", 65);
     $this->db->where("TIMESTAMPDIFF(SECOND,NOW(),CONCAT_WS(' ',e.date,e.show_time)) >", 0);
     $query = $this->db->get();
     //echo $this->db->last_query();
     $result = $query->result();
     if ($result) {
         $i = 0;
         foreach ($query->result() as $key => $val) {
             if (strtolower($val->device_type) == 'ios') {
                 $device_data['ios'][] = $val->device_unique_id;
             } else {
                 if (strtolower($val->device_type) == 'android') {
                     $device_data['android'][] = $val->device_unique_id;
                 }
             }
             $data[$i]['id'] = $val->id;
             $data[$i]['message'] = $val->channel_name;
             $data[$i]['show_time'] = $val->date . ' ' . $val->show_time;
             $data[$i]['device'] = $device_data;
             $i++;
             $device_data = array();
         }
         //--- send notification ---//
         foreach ($data as $row) {
             $device_ids = $device_data;
             $message = $row['message'] . " Event is going to start at " . $row['show_time'];
             //$notification_type = $row->notification_type;
             //$id= $row->id;
             foreach ($device_ids as $key => $value) {
                 if ($key == 'ios') {
                     //for($i=0;$i<count($value);$i++){
                     $deviceToken = $value;
                     $result = apns($deviceToken, $message, $uniquid);
                     //-- helper function
                     /*if(!$result)
                       {
                            //$this->response('Failed to connet GCM', 404);
                       }else{
                           //--- insert in history databse ---//
                           $data_history['push_id'] = $uniquid;
                           $data_history['type'] = 'Push';
                           $data_history['message'] = $message;
                           $data_history['platform'] = 'ios';
                           $data_history['audience'] = $notification_type;
                           $data_history['sent_count'] = count($deviceToken);									
                           //------------------------//
                            //-- insert data --//
                           $this->save($data_history);
                         $new_id = $this->db->insert_id();
                         // $this->response("Message sent successfully", 200);                       
                           //----------------//
                       }*/
                     //}
                 }
                 if ($key == 'android') {
                     $gcmRegIds = $value;
                     //  print_r($gcmRegIds);die;
                     $message = array("m" => $pushMessage);
                     $new_gcmRegIds = array_chunk($gcmRegIds, NOTIFICATION_DEVICE_CHUNK);
                     foreach ($new_gcmRegIds as $gcmIdArray) {
                         $pushStatus = sendMessageThroughGCM($gcmIdArray, $message, $uniquid);
                     }
                     //$pushStatus = sendMessageThroughGCM($gcmRegIds, $message,$uniquid);	//-- helper function --//
                     /* if(!$pushStatus)
                        {
                            $this->response('Failed to connet GCM', 404);
                        }else
                        {
                            //--- insert in history databse ---//
                            $data_history['push_id'] = $uniquid;
                            $data_history['type'] = 'Push';
                            $data_history['message'] = $message;
                            $data_history['platform'] = 'android';
                            $data_history['audience'] = $notification_type;
                            $data_history['sent_count'] = count($gcmRegIds);	
                            //------------------------//
                            //print_r($data_history);die;
                            $this->save($data_history);
                            $new_id = $this->db->insert_id();                                    
                            //----------------//
                           // echo "Notification send successfully";
                        }	*/
                 }
             }
             //echo '<pre>';  print_r($result);die;
             //SELECT * FROM `pushnotification_scheduler` WHERE `schedule_time` < NOW() and `status` = 'pending'
         }
         $this->response(array('code' => 1));
     }
     $this->response(array('code' => 0, 'result' => 'No record found'));
 }
 function index($push_id = '')
 {
     $data['welcome'] = $this;
     $data['time_zones'] = unserialize(TIME_ZONE_LIST);
     if ($push_id != '') {
         $data['push_detail'] = $this->Push_notification_model->get_push_history_detail($push_id);
     }
     //Post message to GCM when submitted
     $pushStatus = "GCM Status Message will appear here";
     //$regId = array('apns'=>'1461aa5ee0613a47a748deb1051c6b2d30378b634418ecf7729ac19f7d95d85e','gcm'=>'APA91bEfsmtpPO6VJBN5gxTxXJvKiUlnEtJG0H56RHf_JpUE63t-YZ2VX1PiJIuFrJBYCDtP_5GwSoBRzUfc0UbWaUR9M04kORnOtqR-r_3hubylp0MqZNhSy03h2lPnUUJCxU7z97_7Wct-168uzwCRv7CAJO5t9Q');
     if ($_POST) {
         //$gcmRegID  = file_get_contents("GCMRegId.txt");
         // $gcmRegID = $_POST["regId"];
         $timestamp = strtotime("now");
         $uniquid = uniqid($timestamp);
         $_POST['keywords'] = explode(',', $_POST['keywords']);
         $pushMessage = $_POST["message"];
         $result = $this->Push_notification_model->push_notification_data($_POST);
         //-- schedule notification ---//
         if ($_POST['delivery_time'] == 2) {
             //example usage
             $date = $_POST['date'];
             //'2015-05-11 21:53:01';
             $time = $_POST['time'];
             $user_timezone = $_POST['timezone'];
             $scheduleDate = $date . ' ' . $time;
             //-- convert user time into server time ---//
             $servertime = convert_to_server_date($scheduleDate, 'Y-m-d H:i:s', $user_timezone);
             $schedule_data['device_ids'] = serialize($result);
             $schedule_data['message'] = $pushMessage;
             $schedule_data['schedule_time'] = $servertime;
             $schedule_data['notification_type'] = $_POST['notification_type'];
             $schedule_data['status'] = 'pending';
             $id = $this->Push_notification_model->schedule_notification($schedule_data);
             if ($id) {
                 $this->session->set_flashdata('message', $this->_successmsg('Notification schedule successfully.'));
                 //redirect('push_notification');
             } else {
                 $this->session->set_flashdata('message', $this->_errormsg('Error scheduling notification'));
             }
             redirect('push_notification');
         }
         //print_r($result);
         //--------------------------//
         if ($result) {
             foreach ($result as $key => $value) {
                 if ($key == 'ios') {
                     //for($i=0;$i<count($value);$i++){
                     $deviceToken = $value;
                     $result = apns($deviceToken, $pushMessage, $uniquid);
                     //-- helper function --//
                     if (!$result) {
                         $this->session->set_flashdata('message', $this->_errormsg("Failed to connect APNS"));
                         redirect('push_notification');
                     } else {
                         //--- insert in history databse ---//
                         $data_history['push_id'] = $uniquid;
                         $data_history['type'] = 'Push';
                         $data_history['message'] = $_POST["message"];
                         $data_history['platform'] = 'ios';
                         $data_history['audience'] = $_POST['notification_type'];
                         $data_history['sent_count'] = count($deviceToken);
                         $this->Push_notification_model->save($data_history);
                         //------------------------//
                     }
                     //}
                 }
                 if ($key == 'android') {
                     $gcmRegIds = $value;
                     //echo '<pre>';  print_r($gcmRegIds); die;
                     $message = array("m" => $pushMessage);
                     $new_gcmRegIds = array_chunk($gcmRegIds, NOTIFICATION_DEVICE_CHUNK);
                     foreach ($new_gcmRegIds as $gcmIdArray) {
                         $pushStatus = sendMessageThroughGCM($gcmIdArray, $message, $uniquid);
                     }
                     //$pushStatus = sendMessageThroughGCM($gcmRegIds, $message,$uniquid);
                     if (!$pushStatus) {
                         $this->session->set_flashdata('message', $this->_errormsg("Failed to connect GCM"));
                         redirect('push_notification');
                     } else {
                         //--- insert in history databse ---//
                         $data_history['push_id'] = $uniquid;
                         $data_history['type'] = 'Push';
                         $data_history['message'] = $_POST["message"];
                         $data_history['platform'] = 'android';
                         $data_history['audience'] = $_POST['notification_type'];
                         $data_history['sent_count'] = count($gcmRegIds);
                         //------------------------//
                         //print_r($data_history);die;
                         //-- insert data --//
                         $this->Push_notification_model->save($data_history);
                     }
                 }
             }
             $this->session->set_flashdata('message', $this->_successmsg('Notification send successfully.'));
         } else {
             $this->session->set_flashdata('message', $this->_errormsg('No record found.'));
         }
         redirect('push_notification');
     }
     //Get Reg ID sent from Android App and store it in text file
     /*if(!empty($_GET["shareRegId"])) {
     		$gcmRegID  = $_POST["regId"]; 
     		file_put_contents("GCMRegId.txt",$gcmRegID);
     		echo "Done!";
     		exit;
     	}    */
     $this->show_view('pushnotification/send', $data);
 }
Ejemplo n.º 3
0
 function push_notification($data)
 {
     $this->load->helper('push');
     $this->load->model('push_notification/Push_notification_model');
     $result = $this->Push_notification_model->push_notification_data($data);
     $timestamp = strtotime("now");
     $uniquid = uniqid($timestamp);
     if ($result) {
         foreach ($result as $key => $value) {
             if ($key == 'ios') {
                 //for($i=0;$i<count($value);$i++){
                 $deviceToken = $value;
                 $result = apns($deviceToken, $data["message"], $uniquid);
                 //-- helper function --//
                 if (!$result) {
                     //$this->response(array('code'=>0,'result'=>'Failed to connect APNS'), 201);
                 } else {
                     //--- insert in history databse ---//
                     $data_history['push_id'] = $uniquid;
                     $data_history['type'] = 'Push';
                     $data_history['message'] = $data["message"];
                     $data_history['platform'] = 'ios';
                     $data_history['audience'] = $data['notification_type'];
                     $data_history['sent_count'] = count($deviceToken);
                     $this->Push_notification_model->save($data_history);
                     //------------------------//
                 }
                 //}
             }
             if ($key == 'android') {
                 $gcmRegIds = $value;
                 //echo '<pre>';  print_r($gcmRegIds); die;
                 $message = array("m" => $pushMessage);
                 $new_gcmRegIds = array_chunk($gcmRegIds, NOTIFICATION_DEVICE_CHUNK);
                 foreach ($new_gcmRegIds as $gcmIdArray) {
                     $pushStatus = sendMessageThroughGCM($gcmIdArray, $message, $uniquid);
                 }
                 //$pushStatus = sendMessageThroughGCM($gcmRegIds, $message,$uniquid);
                 if (!$pushStatus) {
                     //$this->session->set_flashdata('message', $this->_errormsg("Failed to connect GCM"));
                 } else {
                     //--- insert in history databse ---//
                     $data_history['push_id'] = $uniquid;
                     $data_history['type'] = 'Push';
                     $data_history['message'] = $data["message"];
                     $data_history['platform'] = 'android';
                     $data_history['audience'] = $data['notification_type'];
                     $data_history['sent_count'] = count($gcmRegIds);
                     //------------------------//
                     //print_r($data_history);die;
                     //-- insert data --//
                     $this->Push_notification_model->save($data_history);
                 }
             }
         }
     }
 }