PHPackages                             orchideeknopf/laravel-notifications-fcm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. orchideeknopf/laravel-notifications-fcm

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

orchideeknopf/laravel-notifications-fcm
=======================================

FCM (Firebase Cloud Messaging) Notifications Driver for Laravel

1.0.0(6mo ago)05MITPHPPHP ^8.2|^8.3|^8.4

Since Nov 5Pushed 6mo agoCompare

[ Source](https://github.com/OrchideeKnopf/laravel-notifications-fcm)[ Packagist](https://packagist.org/packages/orchideeknopf/laravel-notifications-fcm)[ Docs](https://github.com/laravel-notification-channels/fcm)[ RSS](/packages/orchideeknopf-laravel-notifications-fcm/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

Laravel FCM (Firebase Cloud Messaging) Notification Channel
===========================================================

[](#laravel-fcm-firebase-cloud-messaging-notification-channel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/07629b7f221a0e4eabae0c966c2dd024fef3ab76c84746a72c3616fceadcb81a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c61726176656c2d6e6f74696669636174696f6e2d6368616e6e656c732f66636d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/coreproc/laravel-notification-channel-fcm)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![StyleCI](https://camo.githubusercontent.com/ad24ee26bcd92745afbd72c211a77cd3b18b9acbad6e3dd4389702ed5126f9db/68747470733a2f2f7374796c6563692e696f2f7265706f732f3230393430363732342f736869656c64)](https://styleci.io/repos/209406724)[![Quality Score](https://camo.githubusercontent.com/266e2de3b2df31065e9be378b85510184075047fbf804280c75d13fd07a8ae60/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6c61726176656c2d6e6f74696669636174696f6e2d6368616e6e656c732f66636d2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/laravel-notification-channels/fcm)[![Total Downloads](https://camo.githubusercontent.com/0dd3851de895d9cde604bb40225dc04d4a2498e818723fd13b64631eea7b203d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c61726176656c2d6e6f74696669636174696f6e2d6368616e6e656c732f66636d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laravel-notification-channels/fcm)

This package makes it easy to send notifications using [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/) (FCM) with Laravel.

Contents
--------

[](#contents)

- [Installation](#installation)
    - [Setting up the FCM service](#setting-up-the-fcm-service)
- [Usage](#usage)
    - [Available message methods](#available-message-methods)
    - [Custom clients](#custom-clients)
    - [Handling errors](#handling-errors)
- [Changelog](#changelog)
- [Testing](#testing)
- [Security](#security)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)

Installation
------------

[](#installation)

Install this package with Composer:

```
composer require laravel-notification-channels/fcm
```

### Setting up the FCM service

[](#setting-up-the-fcm-service)

This package now uses the [laravel-firebase](https://github.com/kreait/laravel-firebase) library to authenticate and make the API calls to Firebase. Follow the [configuration](https://github.com/kreait/laravel-firebase#configuration)steps specified in their readme before using this.

After following their configuration steps, make sure that you've specified your `FIREBASE_CREDENTIALS` in your .env file.

Usage
-----

[](#usage)

After setting up your Firebase credentials, you can now send notifications via FCM by a Notification class and sending it via the `FcmChannel::class`. Here is an example:

```
use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;

class AccountActivated extends Notification
{
    public function via($notifiable)
    {
        return [FcmChannel::class];
    }

    public function toFcm($notifiable): FcmMessage
    {
        return (new FcmMessage(notification: new FcmNotification(
                title: 'Account Activated',
                body: 'Your account has been activated.',
                image: 'http://example.com/url-to-image-here.png'
            )))
            ->data(['data1' => 'value', 'data2' => 'value2'])
            ->custom([
                'android' => [
                    'notification' => [
                        'color' => '#0A0A0A',
                        'sound' => 'default',
                    ],
                    'fcm_options' => [
                        'analytics_label' => 'analytics',
                    ],
                ],
                'apns' => [
                    'payload' => [
                        'aps' => [
                            'sound' => 'default'
                        ],
                    ],
                    'fcm_options' => [
                        'analytics_label' => 'analytics',
                    ],
                ],
            ]);
    }
}
```

You will have to set a `routeNotificationForFcm()` method in your notifiable model. This method should return the user's FCM token(s) from storage. For example:

```
class User extends Authenticatable
{
    use Notifiable;

    ...

    /**
     * Specifies the user's FCM token
     *
     * @return string|array
     */
    public function routeNotificationForFcm()
    {
        return $this->fcm_token;
    }
}
```

You can also return an array of tokens to send notifications via multicast to different user devices.

```
class User extends Authenticatable
{
    use Notifiable;

    ...

    /**
     * Specifies the user's FCM tokens
     *
     * @return string|array
     */
    public function routeNotificationForFcm()
    {
        return $this->getDeviceTokens();
    }
}
```

Once you have that in place, you can simply send a notification to the user by doing the following:

```
$user->notify(new AccountActivated);
```

### Available Message methods

[](#available-message-methods)

View the `FcmMessage` source for the complete list of options.

```
FcmMessage::create()
    ->name('name')
    ->token('token')
    ->topic('topic')
    ->condition('condition')
    ->data(['a' => 'b'])
    ->custom(['notification' => []]);
```

Custom clients
--------------

[](#custom-clients)

You can change the underlying Firebase Messaging client on the fly if required. Provide an instance of `Kreait\Firebase\Contract\Messaging` to your FCM message and it will be used in place of the default client.

```
public function toFcm(mixed $notifiable): FcmMessage
{
    $client = app(\Kreait\Firebase\Contract\Messaging::class);

    return FcmMessage::create()->usingClient($client);
}
```

Handling errors
---------------

[](#handling-errors)

When a notification fails it will dispatch an `Illuminate\Notifications\Events\NotificationFailed` event. You can listen for this event and choose to handle these notifications as appropriate. For example, you may choose to delete expired notification tokens from your database.

```
