File manager - Edit - /home/cipherteam/htdocs/cipherteam.in/APPS/CronJobs/IPONEWSFINDERS/senNotification.php
Back
<?php /** * senNotification.php (GET API) * Call like: * senNotification.php?key=PR@TIK@2552&title=Hello&message=Test */ header("Content-Type: application/json"); ini_set('display_errors', 1); error_reporting(E_ALL); /* ================= CONFIG ================= */ $ACCESS_KEY = "PR@TIK@2552"; $FIREBASE_PROJECT_ID = "ipo-admin-app-232d0"; $SERVICE_ACCOUNT_FILE = __DIR__ . "/service-account.json"; $dbHost = "localhost:3306"; $dbUser = "marketdatauser"; $dbPass = "d7o7A8%s2d7"; $dbName = "marketdataci"; /* ================= INPUT (GET) ================= */ $key = $_GET['key'] ?? ''; $title = trim($_GET['title'] ?? ''); $message = trim($_GET['message'] ?? ''); if ($key !== $ACCESS_KEY) { http_response_code(403); echo json_encode(["status" => "error", "msg" => "Invalid API key"]); exit; } if ($title === "" || $message === "") { http_response_code(400); echo json_encode(["status" => "error", "msg" => "title/message missing"]); exit; } if (!file_exists($SERVICE_ACCOUNT_FILE)) { http_response_code(500); echo json_encode(["status" => "error", "msg" => "service-account.json not found"]); exit; } /* ================= HELPERS ================= */ function base64UrlEncode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } function getAccessToken($jsonPath) { $sa = json_decode(file_get_contents($jsonPath), true); if (!isset($sa['client_email'], $sa['private_key'], $sa['token_uri'])) { throw new Exception("Invalid service account JSON"); } $now = time(); $header = ['alg' => 'RS256', 'typ' => 'JWT']; $claims = [ 'iss' => $sa['client_email'], 'scope' => 'https://www.googleapis.com/auth/firebase.messaging', 'aud' => $sa['token_uri'], 'iat' => $now, 'exp' => $now + 3600 ]; $jwt = base64UrlEncode(json_encode($header)) . "." . base64UrlEncode(json_encode($claims)); openssl_sign($jwt, $signature, $sa['private_key'], 'SHA256'); $jwt .= "." . base64UrlEncode($signature); $ch = curl_init($sa['token_uri']); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => http_build_query([ 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $jwt ]), CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'] ]); $res = curl_exec($ch); curl_close($ch); $data = json_decode($res, true); return $data['access_token'] ?? null; } function sendFCM($projectId, $token, $deviceToken, $title, $body) { $url = "https://fcm.googleapis.com/v1/projects/$projectId/messages:send"; $payload = [ "message" => [ "token" => $deviceToken, "notification" => [ "title" => $title, "body" => $body ], "android" => [ "notification" => [ "click_action" => "MAIN" ] ] ] ]; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "Authorization: Bearer $token", "Content-Type: application/json" ], CURLOPT_POSTFIELDS => json_encode($payload) ]); $resp = curl_exec($ch); $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return ["ok" => ($http >= 200 && $http < 300), "http" => $http, "resp" => $resp]; } /* ================= MAIN ================= */ try { $con = mysqli_connect($dbHost, $dbUser, $dbPass, $dbName); if (!$con) throw new Exception("DB connection failed"); $tokens = []; $res = $con->query("SELECT fcmid FROM admin_fcm WHERE fcmid!=''"); while ($row = $res->fetch_assoc()) { $tk = trim($row['fcmid']); if ($tk !== "") $tokens[] = $tk; } $tokens = array_values(array_unique($tokens)); if (count($tokens) === 0) { echo json_encode(["status" => "error", "msg" => "No tokens found"]); exit; } $accessToken = getAccessToken($SERVICE_ACCOUNT_FILE); if (!$accessToken) throw new Exception("Failed to generate OAuth token"); $sent = 0; $failed = 0; foreach ($tokens as $tk) { $r = sendFCM($FIREBASE_PROJECT_ID, $accessToken, $tk, $title, $message); if ($r["ok"]) $sent++; else $failed++; } echo json_encode([ "status" => "success", "sent" => $sent, "failed" => $failed, "total_tokens" => count($tokens) ]); } catch (Exception $e) { http_response_code(500); echo json_encode(["status" => "error", "msg" => $e->getMessage()]); }
| ver. 1.4 |
Github
|
.
| PHP 8.2.9 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings