Advertisement
vdp

Untitled

vdp
Jun 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. define( 'API_ACCESS_KEY', 'FIREBASE_API_KEY_HERE' );
  4. class FamilyModel extends CI_Model{
  5.   public function addFamily($id_user,$id_user_family){
  6.     $this->db->where('id_user',$id_user);
  7.     $this->db->where('id_user_family',$id_user_family);
  8.     $check = $this->db->get('families')->result();
  9.     if($id_user == $id_user_family){
  10.       $reseponse['error'] = true;
  11.       $response['message'] = "You can't add yourself";
  12.     }
  13.     if($check){
  14.       $response['error'] = true;
  15.       $response['message'] = 'Family has been added';
  16.     }else{
  17.       $data = array(
  18.         'id_user'        => $id_user,
  19.         'id_user_family' => $id_user_family
  20.       );
  21.       $insert = $this->db->insert('families',$data);
  22.       if($insert){
  23.         $registrationIds = array();
  24.         $this->db->select('firebase_token, (SELECT name FROM user WHERE id_user = '.$id_user.') AS name_elderly');
  25.         $this->db->where('id_user',$id_user_family);
  26.         $datas = $this->db->get('user')->result();
  27.         foreach($datas as $data){
  28.           array_push($registrationIds,$data->firebase_token);
  29.           $name_elderly = $data->name_elderly;
  30.         }
  31.         $title = $name_elderly.' added you as his/her family';
  32.         $body = 'Click here to check your elderly list';
  33.         $response['notification'] = $this->FamilyModel->sendNotification($registrationIds,$body,$title);
  34.         $response['error'] = false;
  35.         $response['message'] = 'Add Family Success';
  36.       }else{
  37.         $response['error'] = true;
  38.         $response['message'] = 'Add Family Fail';
  39.       }
  40.     }
  41.     return $response;
  42.   }
  43.   public function sendNotification($registrationIds,$body,$title){
  44.       $msg = array
  45.       (
  46.           'body'   => $body,
  47.           'title'     => $title,
  48.           'subtitle'  => "",
  49.           'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
  50.           'vibrate'   => 'default',
  51.           'sound'     => 'default',
  52.           'largeIcon' => 'large_icon',
  53.           'smallIcon' => 'small_icon',
  54.           'badge' => '1',
  55.           'action' => 'FamilyAdded'
  56.       );
  57.       $fields = array
  58.       (
  59.           'registration_ids'  => $registrationIds,
  60.           'data'          => $msg,
  61.           'priority' => 'high',
  62.       );
  63.  
  64.       $headers = array
  65.       (
  66.           'Authorization: key=' . API_ACCESS_KEY,
  67.           'Content-Type: application/json'
  68.       );
  69.       $ch = curl_init();
  70.       curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
  71.       curl_setopt( $ch,CURLOPT_POST, true );
  72.       curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
  73.       curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
  74.       curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
  75.       curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
  76.       $result = curl_exec($ch);
  77.       curl_close( $ch );
  78.       return json_decode($result);
  79.   }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement