<?php

namespace App\Helpers;

use Kreait\Firebase\Exception\FirebaseException;
use Kreait\Firebase\Exception\MessagingException;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;

class FirebaseHelper
{
    /**
     * @throws MessagingException
     * @throws FirebaseException
     */
    public function sendTopic($topic, $title, $body, $data) {

        $factory = (new Factory)->withServiceAccount(storage_path('firebase.json'));
        $messaging = $factory->createMessaging();
        $message = CloudMessage::new()->toTopic($topic)
            ->withNotification(Notification::create($title, $body))
            ->withData($data);

        return $messaging->send($message);
    }
}

