<?php

$token = $_POST['token'];
$body = $_POST['body'];
$title = $_POST['title'];
$image = $_POST['image']; // must be public https URL
$key = $_POST['key'];

$security_key = decrypData($key);

if ($security_key == 'Jobayer121415') {

    require 'get-access-token.php'; // এখানে আপনার access token generator
    $serviceAccountKeyFile = 'service-account-file.json';
    $accessToken = getAccessToken($serviceAccountKeyFile);

    $url = "https://fcm.googleapis.com/v1/projects/money---test/messages:send";

    $dataPayload = array(
        'title' => $title,
        'body' => $body,
        'image' => $image // ইমেজ URL data তে থাকলেও notification এও দিতে হবে
    );

    $notification = array(
        'title' => $title,
        'body' => $body,
        'image' => $image
    );

    $message = array(
        'token' => $token,
        'data' => $dataPayload,
        'notification' => $notification,
        'android' => array(
            'notification' => array(
                'channel_id' => 'vot_finity_channel'
            )
        )
    );

    $json = json_encode(['message' => $message]);

    $headers = array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $accessToken
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($ch);

    if ($response === FALSE) {
        die('FCM Send Error: ' . curl_error($ch));
    }

    curl_close($ch);
    echo $response;
}

function decrypData($text) {
    $decoded = base64_decode($text);
    return openssl_decrypt($decoded, 'AES-128-ECB', '0175526800812345', OPENSSL_RAW_DATA);
}
?>
