<?php
require_once("/home/fundbridge/crons.ballng.com/firebase/vendor/autoload.php");

use Kreait\Firebase;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Database;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\WebPushConfig;
use Kreait\Firebase\Messaging\ApnsConfig;

$factory = (new Firebase\Factory())
    ->withServiceAccount('/home/fundbridge/crons.ballng.com/ball-e3586-firebase-adminsdk-m3c0y-f2cfcab0b9.json');
$messaging = $factory->createMessaging();

if(isset($_POST['users'])){
    extract($_POST);
    $users = json_decode($users, true);
    if($title != ""){
        $image = $image != "" ? "https://assets.ballng.com/notifications/$image" : "";
        $notification = ['title' => $title,'body' => $message,'image' => $image,"tag"=>mt_rand(0,99999),"icon"=>"https://assets.ballng.com/logos/logo_coloured.png"];
        $message = CloudMessage::fromArray(['notification' => $notification,'data' => ['message' => $data]]);
        $config = WebPushConfig::fromArray(['notification' => $notification,'fcm_options' => ['link' => $link]]);
        $message = $message->withWebPushConfig($config);
    }else{
        $message = CloudMessage::fromArray(['data' => ['message' => $data]]);
    }
    
    
    $sendReport = $messaging->sendMulticast($message, $users);
    print_r($sendReport);
}

if(isset($_POST['all'])){
    extract($_POST);
    if($title != ""){
        $image = $image != "" ? "https://assets.ballng.com/notifications/$image" : "";
        $notification = ['title' => $title,'body' => $message,'image' => $image,"tag"=>mt_rand(0,99999)];
        $message = CloudMessage::fromArray(['topic' =>$all,'notification' => $notification,'data' => ['message' => $data]]);
    }else{
        $message = CloudMessage::fromArray(['topic' =>$all,'data' => ['message' => $data]]);
    }
    $messaging->send($message);
}
?>