<?php

include("../db.php"); 


 $session = $_REQUEST["session"];
$msg = $_REQUEST["msg"];
$class = $_REQUEST["class"];
 $set_title = $_REQUEST["title"];
$conwhere='';
if(isset($_REQUEST["section"]) && $_REQUEST["section"]!=""){
		
			$conwhere=" and section='".$_REQUEST["section"]."' ";
		}


// Enabling error reporting
error_reporting(-1);
ini_set('display_errors', 'On');

require_once  __DIR__.'/firebase/firebase.php';
require_once  __DIR__.'/firebase/push.php';

$firebase = new Firebase();
$push = new Push();

$tokenlist=$db->getRows("SELECT idno,name,fcm_token from student where class='$class' $conwhere and session='$session' and status='1' ");

$result_arr = array();

foreach ($tokenlist as $value) {
    //$sql = "SELECT name,fcm_token FROM TABLE WHERE id = '$value'";
  
   //$tokenlist = mysql_query("SELECT name,fcm_token from student where class='$x_value' and session='$session' and status='1' ");
  
   // $retval = mysqli_query($con,$sql);


    //if($tokenlist){

             $stu_id =  $value['idno'];
             $target_user_name =  $value['name'];
            $fb_token =  $value['fcm_token'];
      

       

        if (strlen($fb_token) > 0 ) {
            // optional payload
            $payload = array();
            $payload['name'] = $target_user_name;
            $payload['msg'] = $msg; // whether to include to image or not
             $payload['stu_id'] = $stu_id;
            $include_image = TRUE;
            $push->setTitle($set_title);
            $push->setMessage($msg);
            if ($include_image) {
                $push->setImage('https://api.androidhive.info/images/minion.jpg');
            } else {
                $push->setImage('');
            }
            $push->setIsBackground(FALSE);
            $push->setPayload($payload);
            $json = '';
            $response = '';
            $json = $push->getPush();
       
            $response = $firebase->send($fb_token, $json);
            
            array_push($result_arr,$response);
        }


    //}

}

echo json_encode($result_arr);

?>