<?php

$sender = $_POST['sender'];
$receiver = $_POST['receiver'];
$token = $_POST['token'];
$message = $_POST['message'];
$datetime = $_POST['time'];

sendPushNotification($token, $sender, $message,$datetime);


function sendPushNotification($deviceToken, $title, $body,$time) {
    // FCM server key
    $serverKey = 'AAAAdGlOElM:APA91bELNPs_4odsNIZ_eOaRoLk3TLPRstRXc9yVTrEXqBFh1Y_vbjTvO8zWP5hngh3Scr9Q2KBa13rfoDy6MZrUj0iuNmqJe2a0WssA7AQs5M9Vvim9zERCUUA6X_Axc5H0notjGflz';

    // FCM API URL
    $url = 'https://fcm.googleapis.com/fcm/send';

    // Notification payload
    $notification = array(
        'title' => $title,
        'body' => $body,
        'sound' => 'default',
        'badge' => '1'
    );

    // Data payload (optional)
    $data = array(
        'sender_phone' => $title,
        'message' => $body,
        'time'=>$time
    );

    // Request payload
    $payload = array(
        'to' => $deviceToken,
        'notification' => $notification,
        'data' => $data
    );

    // Headers
    $headers = array(
        'Authorization: key=' . $serverKey,
        'Content-Type: application/json'
    );

    // Send the POST request to FCM API
    $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);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    $result = curl_exec($ch);
    curl_close($ch);

    // Handle the response from FCM API
    if ($result === false) {
        echo 'Failed to send push notification: ' . curl_error($ch);
    } else {
        echo 'Push notification sent successfully.';
    }
}



?>
