{"id":17617705,"url":"https://github.com/ankurk91/fcm-notification-channel","last_synced_at":"2025-04-04T12:08:00.509Z","repository":{"id":40647512,"uuid":"448494696","full_name":"ankurk91/fcm-notification-channel","owner":"ankurk91","description":"All in One Firebase push notification channel for Laravel :bell:","archived":false,"fork":false,"pushed_at":"2024-10-15T14:04:49.000Z","size":92,"stargazers_count":70,"open_issues_count":0,"forks_count":7,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-29T20:45:07.465Z","etag":null,"topics":["fcm","firebase","laravel","notification-channel"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/ankurk91/fcm-notification-channel","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/ankurk91.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","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}},"created_at":"2022-01-16T08:12:38.000Z","updated_at":"2024-10-26T23:47:10.000Z","dependencies_parsed_at":"2023-11-24T13:45:40.561Z","dependency_job_id":"818418cf-a5fa-41e0-a5ce-1f117ebf5fc8","html_url":"https://github.com/ankurk91/fcm-notification-channel","commit_stats":{"total_commits":33,"total_committers":4,"mean_commits":8.25,"dds":0.09090909090909094,"last_synced_commit":"3a138007e9f0be975e172994d837cb10d4172b80"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankurk91%2Ffcm-notification-channel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankurk91%2Ffcm-notification-channel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankurk91%2Ffcm-notification-channel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankurk91%2Ffcm-notification-channel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ankurk91","download_url":"https://codeload.github.com/ankurk91/fcm-notification-channel/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174418,"owners_count":20896078,"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":["fcm","firebase","laravel","notification-channel"],"created_at":"2024-10-22T19:15:11.838Z","updated_at":"2025-04-04T12:08:00.493Z","avatar_url":"https://github.com/ankurk91.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FCM Notification Channel for Laravel\n\n[![Packagist](https://badgen.net/packagist/v/ankurk91/fcm-notification-channel)](https://packagist.org/packages/ankurk91/fcm-notification-channel)\n[![GitHub-tag](https://badgen.net/github/tag/ankurk91/fcm-notification-channel)](https://github.com/ankurk91/fcm-notification-channel/tags)\n[![License](https://badgen.net/packagist/license/ankurk91/fcm-notification-channel)](LICENSE.txt)\n[![Downloads](https://badgen.net/packagist/dt/ankurk91/fcm-notification-channel)](https://packagist.org/packages/ankurk91/fcm-notification-channel/stats)\n[![GH-Actions](https://github.com/ankurk91/fcm-notification-channel/workflows/tests/badge.svg)](https://github.com/ankurk91/fcm-notification-channel/actions)\n[![codecov](https://codecov.io/gh/ankurk91/fcm-notification-channel/branch/main/graph/badge.svg)](https://codecov.io/gh/ankurk91/fcm-notification-channel)\n\nSend [Firebase](https://firebase.google.com/docs/cloud-messaging) push notifications with Laravel php framework.\n\n## Highlights\n\n* Using the latest Firebase HTTP v1 [API](https://firebase.google.com/docs/cloud-messaging/migrate-v1)\n* Send message to a topic or condition :wink:\n* Send message to a specific device or multiple devices (Multicast)\n* Send additional RAW data with notification\n* Supports multiple Firebase projects in single Laravel app:fire:\n* Invalid token handling with event and listeners\n* Fully tested package with automated test cases\n* Powered by battle tested [Firebase php SDK](https://firebase-php.readthedocs.io/) :rocket:\n\n## Installation\n\nYou can install this package via composer:\n\n```bash\ncomposer require \"ankurk91/fcm-notification-channel\"\n```\n\n## Configuration\n\nThis package relies on [laravel-firebase](https://github.com/kreait/laravel-firebase) package to interact with Firebase\nservices. Here is the minimal configuration you need in your `.env` file\n\n```dotenv\n# relative or full path to the Service Account JSON file\nFIREBASE_CREDENTIALS=firebase-credentials.json\n```\n\nYou will need to create a [service account](https://firebase.google.com/docs/admin/setup#initialize-sdk)\nand place the JSON file in your project root.\n\nAdditionally, you can update your `.gitignore` file\n\n```gitignore\n/firebase-credentials*.json\n```\n\n## Usage\n\nYou can use the FCM channel in the `via()` method inside your Notification class:\n\n```php\n\u003c?php\nnamespace App\\Notifications;\n\nuse Illuminate\\Bus\\Queueable;\nuse Illuminate\\Notifications\\Notification;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse NotificationChannels\\FCM\\FCMChannel;\nuse Kreait\\Firebase\\Messaging\\CloudMessage;\n\nclass ExampleNotification extends Notification implements ShouldQueue\n{\n    use Queueable;\n\n    public function via($notifiable): array\n    {\n        return [FCMChannel::class];\n    }\n\n    public function toFCM($notifiable): CloudMessage\n    {\n        return CloudMessage::new()\n            -\u003ewithDefaultSounds()\n            -\u003ewithNotification([\n                'title' =\u003e 'Order shipped',\n                'body' =\u003e 'Your order for laptop is shipped.',\n            ])         \n            -\u003ewithData([\n                'orderId' =\u003e '#123'\n            ]);\n    }    \n}\n```\n\nPrepare your Notifiable model:\n\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Relations\\HasMany;\nuse Illuminate\\Notifications\\Notifiable;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\n\nclass User extends Authenticatable\n{\n    use Notifiable;\n       \n    /**\n    * Assuming that you have a database table which stores device tokens.\n    */\n    public function deviceTokens(): HasMany\n    {\n        return $this-\u003ehasMany(DeviceToken::class);\n    }\n    \n    public function routeNotificationForFCM($notification): string|array|null\n    {\n         return $this-\u003edeviceTokens-\u003epluck('token')-\u003etoArray();\n    }\n    \n    /**\n    * Optional method to determine which message target to use\n    * We will use TOKEN type when not specified\n    * @see \\Kreait\\Firebase\\Messaging\\MessageTarget::TYPES\n    */\n    public function routeNotificationForFCMTargetType($notification): ?string\n    {\n        return \\Kreait\\Firebase\\Messaging\\MessageTarget::TOKEN;\n    }\n    \n    /**\n    * Optional method to determine which Firebase project to use\n    * We will use default project when not specified\n    */\n    public function routeNotificationForFCMProject($notification): ?string\n    {\n        return config('firebase.default');\n    }   \n}\n```\n\n## Send to a topic or condition\n\nThis package is not limited to sending notification to tokens.\n\nYou can use Laravel's [on-demand](https://laravel.com/docs/9.x/notifications#on-demand-notifications) notifications to\nsend push notification to a topic or condition or multiple tokens.\n\n```php\n\u003c?php\n\nuse Illuminate\\Support\\Facades\\Notification;\nuse Kreait\\Firebase\\Messaging\\MessageTarget;\nuse App\\Notification\\ExampleNotification;\n\nNotification::route('FCM', 'topicA')\n    -\u003eroute('FCMTargetType', MessageTarget::TOPIC)\n    -\u003enotify(new ExampleNotification());\n\nNotification::route('FCM', \"'TopicA' in topics\")\n    -\u003eroute('FCMTargetType', MessageTarget::CONDITION)\n    -\u003enotify(new ExampleNotification());\n\nNotification::route('FCM', ['token_1', 'token_2'])\n    -\u003eroute('FCMTargetType', MessageTarget::TOKEN)\n    -\u003enotify(new ExampleNotification());\n```\n\n## Events\n\nYou can consume Laravel's inbuilt notification [events](https://laravel.com/docs/9.x/notifications#notification-events)\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\nuse Illuminate\\Notifications\\Events;\nuse Illuminate\\Foundation\\Support\\Providers\\EventServiceProvider as ServiceProvider;\n\nclass EventServiceProvider extends ServiceProvider\n{\n    protected $listen = [\n        Events\\NotificationSent::class =\u003e [\n            //\\App\\Listeners\\FCMNotificationSent::class,\n        ],\n        Events\\NotificationFailed::class =\u003e [\n            \\App\\Listeners\\FCMNotificationFailed::class,\n        ],\n    ];    \n}\n```\n\nHere is the example of the failed event listener class\n\n```php\n\u003c?php\n\nnamespace App\\Listeners;\n\nuse App\\Models\\User;\nuse Illuminate\\Support\\Arr;\nuse NotificationChannels\\FCM\\FCMChannel;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Kreait\\Laravel\\Firebase\\Facades\\Firebase;\nuse Illuminate\\Notifications\\Events\\NotificationFailed;\n\nclass FCMNotificationFailed implements ShouldQueue\n{\n    public function handle(NotificationFailed $event)\n    {\n        if ($event-\u003echannel !== FCMChannel::class) {\n            return;\n        }\n\n        /** @var User $user */\n        $user = $event-\u003enotifiable;\n        \n        $invalidTokens = $this-\u003efindInvalidTokens($user);\n        if (count($invalidTokens)) {           \n            $user-\u003edeviceTokens()-\u003ewhereIn('token', $invalidTokens)-\u003edelete();\n        }\n    }\n    \n    protected function findInvalidTokens(User $user): array\n    {\n        $tokens = Arr::wrap($user-\u003erouteNotificationFor('FCM'));\n        if (! count($tokens)) {\n            return [];\n        }\n\n        $project = $user-\u003erouteNotificationFor('FCMProject');\n        $response = Firebase::project($project)-\u003emessaging()-\u003evalidateRegistrationTokens($tokens);\n\n        return array_unique(array_merge($response['invalid'], $response['unknown']));\n    }\n}\n```\n\nRead more about validating device\ntokens [here](https://firebase-php.readthedocs.io/en/stable/cloud-messaging.html#validating-registration-tokens)\n\nThen; you may want to ignore this exception in your `app/Exceptions/Handler.php`\n\n```php\nprotected $dontReport = [\n    \\NotificationChannels\\FCM\\Exception\\InvalidRecipientException::class,\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 issue, please email `pro.ankurk1[at]gmail[dot]com` instead of using the issue tracker.\n\n### Attribution\n\nThe package is based on [this](https://github.com/kreait/laravel-firebase/pull/69) rejected PR\n\n### License\n\nThis package is licensed under [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fankurk91%2Ffcm-notification-channel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fankurk91%2Ffcm-notification-channel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fankurk91%2Ffcm-notification-channel/lists"}