<?php

  
$mysqli = new mysqli("localhost","techiu33_ConnectincMLTL","techiu33_ConnectincMLTL","techiu33_ConnectincMLTL");

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}





  
//////// for first time start here

  $sql1="SELECT * FROM users WHERE  type = 'USER'";
  $result1= $mysqli->query($sql1);
   

while($row1 = mysqli_fetch_assoc($result1)){
    
      
  
     
       $user_id = $row1['id'];
        $current_time = date('Y-m-d H:i:s');
        
        
  $this_week = "SELECT* FROM lots_sale_request WHERE user_id = '$user_id' AND lots_sale_request.date_time > DATE_SUB(CURDATE(), INTERVAL 15 DAY";

if($this_week){
    $this_week = COUNT(id);
}else{
    $this_week = 0;
}

 
if($this_week > 0){}else{
    
    

         
         
  $sql_u="SELECT * FROM users WHERE  id = '$user_id'";
  $result_u= $mysqli->query($sql_u);
  $row_u = mysqli_fetch_assoc($result_u);
 
 
 

$sql_u_noti = "INSERT INTO notification (user_id, title, message, notification_type)
VALUES ('$user_id', 'Reminder About Selling', 'Every 2 weeks that they go without selling that they have this reminder so that they do not have their account suspended', 'Admin')";


 $sql_u_noti_u = $mysqli->query($sql_u_noti);
 ///////////
  
  
  
                      // send notification for Andriod

 
               

            $user_message_apk = array(
                           "message" => array(
                             "result" => "successful",
                             "key" => "Reminder About Selling",
                             "title" =>"Reminder About Selling",
                             "alert" => "Reminder About Selling",
                             "message"=>"Reminder About Selling', 'Every 2 weeks that they go without selling that they have this reminder so that they do not have their account suspended",
                             "userid" => $row_u['id'],
              "receiver_id" => '',
              "request_id" => '',

                             "user_name" => $row_u['first_name'],
                             "user_phone" =>$row_u['mobile'],
                             "user_lat" => $row_u['lat'],
                             "user_lon" => $row_u['lon'],
                             "user_image" => SITE_URL.'uploads/images/'.$row_u['image'],


                             "date"=> date('Y-m-d h:i:s')
                           )
                         );
                         
                                
                       //print_r($user_message_apk);die;

                        $register_userid = array($row_u['register_id']);


                        user_apk_notification($register_userid, $user_message_apk);
                        ios_user_notification_new($row_u['ios_register_id'],$user_message_apk['message']);



              // end send notification for Andriod   
               
  
           
           
       
           
}  
}
     
//////// for first time end here


  
   function user_apk_notification($registration_ids, $message)
			{

			// Set POST variables
	        //	print_r($message); die;
				$url = 'https://fcm.googleapis.com/fcm/send';
	$fields = array(
		'registration_ids' => $registration_ids,
		'data' => $message,
	);
			$headers = array(
			   // 'Authorization: key=' . "AAAAXLg-IjI:APA91bFBor8ZpreSx_CBQEQ5EUlT2IfbNi-aknCLa4sVvxUbrhi1xJXRV_htcgCz5OVWwrvkGPHh6tQiqQJOe_brvD1R2UYyXWlCPkfR2HThrpHht23ExZ3rRue1LlZS_ImqC7u5a5MN",
			  'Authorization: key=' . "AAAAg7Nkba8:APA91bEUP7inHk8p6_3xIfsWCtsIiq-awypwlQ7IETtrKhQpifndWVxPh6wuWDPNSGcGupj8Se2lLG43uVfjHHxD_CA_o69nDtw4j4sWxfwdr06Uiz22KTH1JZfPM_LRq9mpbkSIuFN7",
				'Content-Type: application/json'
			);
			
			

			// print_r($headers);
			// Open connection

			$ch = curl_init();

			// Set the url, number of POST vars, POST data

			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

			// Disabling SSL Certificate support temporarly

			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

			// Execute post

			$result = curl_exec($ch);
			if ($result === FALSE)
				{
				die('Curl failed: ' . curl_error($ch));
				}

			// Close connection

			curl_close($ch);

		//	echo $result; 

			}



 
       

function ios_user_notification_new($token,$message)
	{//echo $token;
        $ch = curl_init("https://fcm.googleapis.com/fcm/send");
    
        $key = "AAAAg7Nkba8:APA91bEUP7inHk8p6_3xIfsWCtsIiq-awypwlQ7IETtrKhQpifndWVxPh6wuWDPNSGcGupj8Se2lLG43uVfjHHxD_CA_o69nDtw4j4sWxfwdr06Uiz22KTH1JZfPM_LRq9mpbkSIuFN7";
    
        //Creating the notification array.
        $notification = array(
            'title' =>$message['title'],
            'text' => $message['message'],
            'sound' => 'default',
            'badge' => '1',
            );
    
        //This array contains, the token and the notification. The 'to' attribute stores the token.
        $fields = array('to' => $token, 'notification' => $notification,'priority'=>'high','badge'=>'1','sound' => 'default');
    
        //Setup headers:
        $headers = array();
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Authorization: key= '.$key; // key here
    
        $url = "https://fcm.googleapis.com/fcm/send";
        $ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_POST, true);
    	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	// Disabling SSL Certificate support temporarly
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
	    $result = curl_exec($ch);
	    curl_close($ch);
	      //	echo $result;die;
        return $result;
        
        
	}





?>