百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

fcm

> 后端框架
开源

Firebase 云消息 (FCM) 通知通道 (适用于 Laravel)

604 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Firebase 云消息 (FCM) 通知通道 (适用于 Laravel)

Laravel FCM (Firebase Cloud Messaging) Notification Channel

This package makes it easy to send notifications using Firebase Cloud Messaging (FCM) with Laravel.

Contents

  • Installation
    • Setting up the FCM service
  • Usage
    • Available message methods
    • Topic notifications
    • Custom clients
    • Handling errors
  • Changelog
  • Testing
  • Security
  • Contributing
  • Credits
  • License

Installation

Install this package with Composer:

bash
composer require laravel-notification-channels/fcm

Setting up the FCM service

This package now uses the laravel-firebase library to authenticate and make the API calls to Firebase. Follow the 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

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:

…

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:

php
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.

php
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:

php
$user->notify(new AccountActivated);

Available Message methods

View the FcmMessage source for the complete list of options.

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

Topic notifications

You can also send FCM notifications to topics with the FcmTopicChannel. Use an on-demand notification to to route the notification to the topic, or you can set the topic on the message itself in the toFcm method.

php
use NotificationChannels\Fcm\FcmTopicChannel;

Notification::route(FcmTopicChannel::class, 'news')
    ->notify(new BlogCreated($blog));

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.

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

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

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.

php
channel == FcmChannel::class) {

            $report = Arr::get($event->data, 'report');

            $target = $report->target();

            $event->notifiable->notificationTokens()
                ->where('push_token', $target->value())
                ->delete();
        }
    }
}

Remember to register your event listeners in the event service provider.

php
/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    \Illuminate\Notifications\Events\NotificationFailed::class => [
        \App\Listeners\DeleteExpiredNotificationTokens::class,
    ],
];

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

bash
composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

  • Chris Bautista
  • All Contributors

License

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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPandroidfcmfirebasefirebase-cloud-messaging

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架