Skip to content

Laravel Google FCM (Google Firebase Cloud Messaging) Notification Channel (Google Push Notification)

License

Notifications You must be signed in to change notification settings

RajTechnologiesPvtLtd/laravel-fcm-notification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel FCM Notification

Laravel FCM (Firebase Cloud Messaging) Notification Channel

Use this package to send push notifications via Laravel to Firebase Cloud Messaging. Laravel 5.5+ required.

This package works only with Legacy HTTP Server Protocol

Install

This package can be installed through Composer.

composer require rajtechnologies/laravel-fcm-notification

If installing on < Laravel 5.5 then add the service provider:

// config/app.php
'providers' => [
    ...
    RajTechnologies\FCM\FcmNotificationServiceProvider::class,
    ...
];

Add your Firebase API Key in config/services.php.

return [
   
    ...
    ...
    /*
     * Add the Firebase API key
     */
    'fcm' => [
        'key' => env('FCM_SECRET_KEY')
     ]
];

Model of FCM

use RajTechnologies\FCM\Models\FCM;

Change in User Model

    public function fcm()
    {
        return $this->hasMany(FCM::class);
    }

Example Usage

Use Artisan to create a notification:

php artisan make:notification SomeNotification

Return [fcm] in the public function via($notifiable) method of your notification:

public function via($notifiable)
{
    return ['fcm'];
}

Add the method public function toFcm($notifiable) to your notification, and return an instance of FcmMessage:

use RajTechnologies\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
    
    return $message;
}

OR Add User Model Functions

When sending to specific device using Modal, make sure your notifiable entity has routeNotificationForFcm method defined:

Add Method For Notification Function

public function routeNotificationForFcm($notification)
    {
        //For Single Token Only
        /*
        if($this->fcm){
            return $this->fcm[0]->token;
        }
        */
        if($this->fcm){
            $array = $this->fcm->pluck('token')->toArray();
        return implode(',',$array);
        }
        return null;
    }

Add Static Method For Update Token

public static function updateFCM($user_id,$fcm_token){
        $fcmQuery = FCM::query();
        $fcm = $fcmQuery->where('user_id',$user_id)->first();
        if($fcm){
            $fcm->update([
                "token" =>$fcm_token
            ]);
            return true;
        }
        $fcmQuery->create([
            "user_id" =>$user_id,
            "token" =>$fcm_token
        ]);
        return true;
    }

When sending to a topic, you may define so within the toFcm method in the notification:

use RajTechnologies\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->to('the-topic', $recipientIsTopic = true)
    ->content([...])
    ->data([...]);
    
    return $message;
}

Or when sending with a condition:

use RajTechnologies\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->contentAvailable(true)
        ->priority('high')
        ->condition("'user_".$notifiable->id."' in topics")
        ->data([...]);
    
    return $message;
}

You may provide optional headers or override the request headers using setHeaders():

use RajTechnologies\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->setHeaders([
        'project_id'    =>  "123456789"   // FCM sender_id
    ])->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
    
    return $message;
}

Interpreting a Response

To process any laravel notification channel response check Laravel Notification Events

This channel return a json array response:

 {
    "multicast_id": "number",
    "success": "number",
    "failure": "number",
    "canonical_ids": "number",
    "results": "array"
 }

Check FCM Legacy HTTP Server Protocol for response interpreting documentation.

License

The MIT License (MIT). Please see License File for more information.

First Inspiration of Admin Panel

Releases

No releases published

Packages

No packages published

Languages