Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 21
FirebaseCloudMessaging
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 21
 init
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 sendFirebaseNotification
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 19
<?php
namespace App\Http\Controllers\FirebaseController;
use App\Models\Auth\User;
use Illuminate\Http\Request;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Factory;
use Exception;
use Kreait\Firebase\Messaging\ApnsConfig;
use App\Models\NotificationModel\Notification as SystemNotification;
class FirebaseCloudMessaging
{
    public function init(){
        $factory = (new Factory)->withServiceAccount(app_path() . "/Http/Controllers/FirebaseController/firebase-key.json");
        return $factory->createMessaging();
    }
    function sendFirebaseNotification($title="Title 1",$body="Body 1",$userid=3)
    {
        $firebaseTokenModelArray = User::where('id','=',$userid)->get('firebase_token');
        if(empty($firebaseTokenModelArray) || $firebaseTokenModelArray==null || count($firebaseTokenModelArray)==0){
            return;
        }
        $firebaseToken = $firebaseTokenModelArray[0]->firebase_token;
        if(empty($firebaseToken)){
            return;
        }
        $messaging = $this->init();
        $notifications  = SystemNotification::where('notifiable_id', $userid)
                                        ->whereNull('read_at')->count();
        $message = CloudMessage::withTarget('token', $firebaseToken)
            ->withNotification(Notification::create($title, $body))
            ->withHighestPossiblePriority()
            ->withData(['key' => 'value'])
            ->withApnsConfig(
                ApnsConfig::new()
                    ->withBadge($notifications)
            );
        try{
        $messaging->send($message);
        }catch(Exception $e){
        }
    }
}