<?php
function sendNotification($order_id,$user_id,$type){
    $mini = mysqli_connect("168.119.1.58", "maxemcangroup_mm", "*TQ~i3pVAH4m", "maxemcangroup_mm");
    mysqli_query($mini,"set character_set_server='utf8'");
    mysqli_query($mini,"set names 'utf8'");
    
    $query2 = $mini->query("SELECT `device_token` FROM `users` WHERE `phone` = '34333234' LIMIT 1");
    while($data2 = mysqli_fetch_assoc($query2))
    {
        $token = $data2['device_token'];
    }
    
    $serverKey = 'AAAAFsbAZoM:APA91bEilOJI4qXt5zOE4-OnE48LGVddYT5Rg7hILMgyLKKhoLhAYh4Avtp4JEA0x6XuejpAI_bmy8GIhBHoCA9Oyw712nMzcU-k3qyaAxrkA2fEQ0Xym7nFX8RQaxleFZf81ZolypSb';
    $title = "Max-Mini";
    $arr = [
        "order_id" => $order_id,
        "client_id" => $user_id,
        "type" => $type
    ];
    $notification = array('title' => $title , 'body' => 'New Order' , 'sound' => 'default');
    $arrayToSend = array('to' => $token, 'notification' => $notification, 'priority' => 'high', "data" => $arr, "content_available" => true);
    $json = json_encode($arrayToSend);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key=' . $serverKey;
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); //For firebase, use https://fcm.googleapis.com/fcm/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);

    //Send the request
    $response = curl_exec($ch);
    //Close request
    if ($response === false) {
        die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
    // echo die(var_dump($phone));
    // var_dump($phone);
}
