{"id":20696066,"url":"https://github.com/laravel-notification-channels/fcm","last_synced_at":"2025-05-15T04:05:54.014Z","repository":{"id":41322173,"uuid":"209406724","full_name":"laravel-notification-channels/fcm","owner":"laravel-notification-channels","description":"Firebase Cloud Messaging (FCM) notifications channel for Laravel","archived":false,"fork":false,"pushed_at":"2025-02-26T20:58:22.000Z","size":174,"stargazers_count":529,"open_issues_count":1,"forks_count":129,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-14T05:55:19.007Z","etag":null,"topics":["android","fcm","firebase","firebase-cloud-messaging","laravel","notification-channel","notification-icon"],"latest_commit_sha":null,"homepage":"https://laravel-notification-channels.com/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/laravel-notification-channels.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-09-18T21:25:31.000Z","updated_at":"2025-04-09T14:37:43.000Z","dependencies_parsed_at":"2024-03-13T03:29:59.171Z","dependency_job_id":"92c2bcb0-2b0d-4a67-a391-d93e987002cb","html_url":"https://github.com/laravel-notification-channels/fcm","commit_stats":{"total_commits":101,"total_committers":39,"mean_commits":2.58974358974359,"dds":0.7029702970297029,"last_synced_commit":"cd31922512bca870b713425571aa49da2bdba15a"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravel-notification-channels%2Ffcm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravel-notification-channels%2Ffcm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravel-notification-channels%2Ffcm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravel-notification-channels%2Ffcm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laravel-notification-channels","download_url":"https://codeload.github.com/laravel-notification-channels/fcm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254270646,"owners_count":22042859,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","fcm","firebase","firebase-cloud-messaging","laravel","notification-channel","notification-icon"],"created_at":"2024-11-17T00:12:25.404Z","updated_at":"2025-05-15T04:05:49.001Z","avatar_url":"https://github.com/laravel-notification-channels.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel FCM (Firebase Cloud Messaging) Notification Channel\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/fcm.svg?style=flat-square)](https://packagist.org/packages/coreproc/laravel-notification-channel-fcm)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![StyleCI](https://styleci.io/repos/209406724/shield)](https://styleci.io/repos/209406724)\n[![Quality Score](https://img.shields.io/scrutinizer/g/laravel-notification-channels/fcm.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/fcm)\n[![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/fcm.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/fcm)\n\nThis package makes it easy to send notifications using [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/) (FCM) with Laravel.\n\n## Contents\n\n- [Installation](#installation)\n\t- [Setting up the FCM service](#setting-up-the-fcm-service)\n- [Usage](#usage)\n\t- [Available message methods](#available-message-methods)\n    - [Custom clients](#custom-clients)\n    - [Handling errors](#handling-errors)\n- [Changelog](#changelog)\n- [Testing](#testing)\n- [Security](#security)\n- [Contributing](#contributing)\n- [Credits](#credits)\n- [License](#license)\n\n\n## Installation\n\nInstall this package with Composer:\n\n```bash\ncomposer require laravel-notification-channels/fcm\n```\n\n### Setting up the FCM service\n\nThis package now uses the [laravel-firebase](https://github.com/kreait/laravel-firebase) library to authenticate and \nmake the API calls to Firebase. Follow the [configuration](https://github.com/kreait/laravel-firebase#configuration)\nsteps specified in their readme before using this.\n\nAfter following their configuration steps, make sure that you've specified your `FIREBASE_CREDENTIALS` in your .env \nfile. \n\n## Usage\n\nAfter setting up your Firebase credentials, you can now send notifications via FCM by a Notification class and sending\nit via the `FcmChannel::class`. Here is an example:\n\n```php\nuse Illuminate\\Notifications\\Notification;\nuse NotificationChannels\\Fcm\\FcmChannel;\nuse NotificationChannels\\Fcm\\FcmMessage;\nuse NotificationChannels\\Fcm\\Resources\\Notification as FcmNotification;\n\nclass AccountActivated extends Notification\n{\n    public function via($notifiable)\n    {\n        return [FcmChannel::class];\n    }\n\n    public function toFcm($notifiable): FcmMessage\n    {\n        return (new FcmMessage(notification: new FcmNotification(\n                title: 'Account Activated',\n                body: 'Your account has been activated.',\n                image: 'http://example.com/url-to-image-here.png'\n            )))\n            -\u003edata(['data1' =\u003e 'value', 'data2' =\u003e 'value2'])\n            -\u003ecustom([\n                'android' =\u003e [\n                    'notification' =\u003e [\n                        'color' =\u003e '#0A0A0A',\n                        'sound' =\u003e 'default',\n                    ],\n                    'fcm_options' =\u003e [\n                        'analytics_label' =\u003e 'analytics',\n                    ],\n                ],\n                'apns' =\u003e [\n                    'payload' =\u003e [\n                        'aps' =\u003e [\n                            'sound' =\u003e 'default'\n                        ],\n                    ],\n                    'fcm_options' =\u003e [\n                        'analytics_label' =\u003e 'analytics',\n                    ],\n                ],\n            ]);\n    }\n}\n```\n\nYou will have to set a `routeNotificationForFcm()` method in your notifiable model.\nThis method should return the user's FCM token(s) from storage.\nFor example:\n\n```php\nclass User extends Authenticatable\n{\n    use Notifiable;\n\n    ...\n\n    /**\n     * Specifies the user's FCM token\n     *\n     * @return string|array\n     */\n    public function routeNotificationForFcm()\n    {\n        return $this-\u003efcm_token;\n    }\n}\n```\n\nYou can also return an array of tokens to send notifications via multicast to different user devices.\n\n```php\nclass User extends Authenticatable\n{\n    use Notifiable;\n\n    ...\n\n    /**\n     * Specifies the user's FCM tokens\n     *\n     * @return string|array\n     */\n    public function routeNotificationForFcm()\n    {\n        return $this-\u003egetDeviceTokens();\n    }\n}\n```\n\nOnce you have that in place, you can simply send a notification to the user by doing the following:\n\n```php\n$user-\u003enotify(new AccountActivated);\n```\n\n### Available Message methods\n\nView the `FcmMessage` source for the complete list of options.\n\n```php\nFcmMessage::create()\n    -\u003ename('name')\n    -\u003etoken('token')\n    -\u003etopic('topic')\n    -\u003econdition('condition')\n    -\u003edata(['a' =\u003e 'b'])\n    -\u003ecustom(['notification' =\u003e []]);\n```\n\n## Custom clients\n\nYou 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.\n\n```php\npublic function toFcm(mixed $notifiable): FcmMessage\n{\n    $client = app(\\Kreait\\Firebase\\Contract\\Messaging::class);\n\n    return FcmMessage::create()-\u003eusingClient($client);\n}\n```\n\n## Handling errors\n\nWhen 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.\n\n```php\n\u003c?php\n\nnamespace App\\Listeners;\n\nuse Illuminate\\Notifications\\Events\\NotificationFailed;\nuse Illuminate\\Support\\Arr;\n\nclass DeleteExpiredNotificationTokens\n{\n    /**\n     * Handle the event.\n     */\n    public function handle(NotificationFailed $event): void\n    {\n        $report = Arr::get($event-\u003edata, 'report');\n\n        $target = $report-\u003etarget();\n\n        $event-\u003enotifiable-\u003enotificationTokens()\n            -\u003ewhere('push_token', $target-\u003evalue())\n            -\u003edelete();\n    }\n}\n```\n\nRemember to register your event listeners in the event service provider.\n\n```php\n/**\n * The event listener mappings for the application.\n *\n * @var array\n */\nprotected $listen = [\n    \\Illuminate\\Notifications\\Events\\NotificationFailed::class =\u003e [\n        \\App\\Listeners\\DeleteExpiredNotificationTokens::class,\n    ],\n];\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Security\n\nIf you discover any security related issues, please email chrisbjr@gmail.com instead of using the issue tracker.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Credits\n\n- [Chris Bautista](https://github.com/chrisbjr)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaravel-notification-channels%2Ffcm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaravel-notification-channels%2Ffcm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaravel-notification-channels%2Ffcm/lists"}