Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- define( 'API_ACCESS_KEY', 'FIREBASE_API_KEY_HERE' );
- class FamilyModel extends CI_Model{
- public function addFamily($id_user,$id_user_family){
- $this->db->where('id_user',$id_user);
- $this->db->where('id_user_family',$id_user_family);
- $check = $this->db->get('families')->result();
- if($id_user == $id_user_family){
- $reseponse['error'] = true;
- $response['message'] = "You can't add yourself";
- }
- if($check){
- $response['error'] = true;
- $response['message'] = 'Family has been added';
- }else{
- $data = array(
- 'id_user' => $id_user,
- 'id_user_family' => $id_user_family
- );
- $insert = $this->db->insert('families',$data);
- if($insert){
- $registrationIds = array();
- $this->db->select('firebase_token, (SELECT name FROM user WHERE id_user = '.$id_user.') AS name_elderly');
- $this->db->where('id_user',$id_user_family);
- $datas = $this->db->get('user')->result();
- foreach($datas as $data){
- array_push($registrationIds,$data->firebase_token);
- $name_elderly = $data->name_elderly;
- }
- $title = $name_elderly.' added you as his/her family';
- $body = 'Click here to check your elderly list';
- $response['notification'] = $this->FamilyModel->sendNotification($registrationIds,$body,$title);
- $response['error'] = false;
- $response['message'] = 'Add Family Success';
- }else{
- $response['error'] = true;
- $response['message'] = 'Add Family Fail';
- }
- }
- return $response;
- }
- public function sendNotification($registrationIds,$body,$title){
- $msg = array
- (
- 'body' => $body,
- 'title' => $title,
- 'subtitle' => "",
- 'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
- 'vibrate' => 'default',
- 'sound' => 'default',
- 'largeIcon' => 'large_icon',
- 'smallIcon' => 'small_icon',
- 'badge' => '1',
- 'action' => 'FamilyAdded'
- );
- $fields = array
- (
- 'registration_ids' => $registrationIds,
- 'data' => $msg,
- 'priority' => 'high',
- );
- $headers = array
- (
- 'Authorization: key=' . API_ACCESS_KEY,
- 'Content-Type: application/json'
- );
- $ch = curl_init();
- curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
- curl_setopt( $ch,CURLOPT_POST, true );
- curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
- curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
- curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
- curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
- $result = curl_exec($ch);
- curl_close( $ch );
- return json_decode($result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement