risingsun/laravel-notification-channels-fcm

FCM (Firebase Cloud Messaging) 通知驱动程序,用于Laravel

v2.2.7 2021-03-04 07:31 UTC

This package is auto-updated.

Last update: 2024-09-04 15:10:01 UTC


README

Latest Version on Packagist Software License StyleCI Quality Score Total Downloads

此包使您能够使用 Firebase Cloud Messaging (FCM)(适用于 Laravel 5.5+、6.x、7.x 和 8.x)轻松发送通知。

版本 2 发布(2020年3月4日)

V2.0.0 已发布,并将 FCM API 调用从旧的 HTTP 迁移到 HTTP v1(Firebase 文档见此处)。这是一个破坏性更改,因此使用 v1.x 的通知不应升级到此包的 v2.x 版本,除非您计划迁移您的通知类。

内容

安装

使用 Composer 安装此包

composer require laravel-notification-channels/fcm:~2.0

设置 FCM 服务

此包现在使用 laravel-firebase 库进行身份验证并调用 Firebase API。在使用之前,请按照他们的说明文件中指定的配置步骤操作。

按照他们的配置步骤操作后,请确保您已经在您的 .env 文件中指定了您的 FIREBASE_CREDENTIALS

用法

设置好 Firebase 凭证后,您现在可以通过 Notification 类发送通知并使用 FcmChannel::class 发送,以下是一个示例:

use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\AndroidConfig;
use NotificationChannels\Fcm\Resources\AndroidFcmOptions;
use NotificationChannels\Fcm\Resources\AndroidNotification;
use NotificationChannels\Fcm\Resources\ApnsConfig;
use NotificationChannels\Fcm\Resources\ApnsFcmOptions;

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

    public function toFcm($notifiable)
    {
        return FcmMessage::create()
            ->setData(['data1' => 'value', 'data2' => 'value2'])
            ->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
                ->setTitle('Account Activated')
                ->setBody('Your account has been activated.')
                ->setImage('http://example.com/url-to-image-here.png'))
            ->setAndroid(
                AndroidConfig::create()
                    ->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
                    ->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
            )->setApns(
                ApnsConfig::create()
                    ->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
    }

    // optional method when using kreait/laravel-firebase:^3.0, this method can be omitted, defaults to the default project
    public function fcmProject($notifiable, $message)
    {
        // $message is what is returned by `toFcm`
        return 'app'; // name of the firebase project to use
    }
}

您必须在您的可通知模型中设置一个 routeNotificationForFcm() 方法。例如:

class User extends Authenticatable
{
    use Notifiable;

    ....

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

您还可以返回一个令牌数组,以向不同用户的设备发送多播通知。

class User extends Authenticatable
{
    use Notifiable;

    ....

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

一旦设置好,您可以通过以下方式简单地向用户发送通知

$user->notify(new AccountActivated);

可用的消息方法

FcmMessage 类包含以下用于定义有效载荷的方法。所有这些方法都与 FCM API 文档中定义的可用有效载荷相对应。请参阅此链接以查找您可以在 FCM 通知中设置的所有可用数据(此处)

setName(string $name)
setData(array $data)
setNotification(\NotificationChannels\Fcm\Resources\Notification $notification)
setAndroid(NotificationChannels\Fcm\Resources\AndroidConfig $androidConfig)
setApns(NotificationChannels\Fcm\Resources\ApnsConfig $apnsConfig)
setWebpush(NotificationChannels\Fcm\Resources\WebpushConfig $webpushConfig)
setFcmOptions(NotificationChannels\Fcm\Resources\FcmOptions $fcmOptions)
setTopic(string $topic)
setCondition(string $condition)

变更日志

请参阅 CHANGELOG 以获取有关最近更改的更多信息。

测试

$ composer test

安全性

如果您发现任何与安全性相关的问题,请通过电子邮件 chrisbjr@gmail.com 而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 CONTRIBUTING

鸣谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅许可文件