<?php
require __DIR__ . '/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\AndroidConfig;
try {
// Initialize Firebase with service account
$factory = (new Factory)->withServiceAccount(__DIR__ . '/firebase-service-account.json');
$messaging = $factory->createMessaging();
// Device FCM Token
$deviceToken = 'eeRC8-X_QcurZwVvssrSCf:APA91bEK_HrxYMbiUCu2ntF21hqB5uv5w6GTet_J3cMtA-QJAhrRvuwlrlJMM2Sfr5wwkVV-tpcPtB34Gd1IofOMz5078G64XufPbnTZ49p2rRuixXjmW_c'; // Replace with actual token
// Notification title and body
$notification = Notification::create('Important Update', 'This alert is sent via a specific channel');
// Android config with specific channel_id
$androidConfig = AndroidConfig::fromArray([
'notification' => [
'channel_id' => 'palsarh', // MUST match Android app's channel ID
//'click_action' => 'FLUTTER_NOTIFICATION_CLICK', // Optional for Flutter apps
],
]);
// Create the message with target, notification, and config
$message = CloudMessage::withTarget('token', $deviceToken)
->withNotification($notification)
->withAndroidConfig($androidConfig);
// Send the notification
$messaging->send($message);
echo "Notification sent to specific channel ID!";
} catch (\Throwable $e) {
echo 'Error: ' . $e->getMessage();
}