FCM (Firebase Cloud Messaging) 通知驱动程序,适用于Laravel 5.5+、6.x和7.x

2.0.2 2020-03-30 15:48 UTC

This package is auto-updated.

Last update: 2024-09-10 23:56:54 UTC


README

Latest Version on Packagist Software License StyleCI Quality Score Total Downloads

此包使得使用Firebase Cloud Messaging (FCM)通过Laravel 5.5+、6.x和7.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调用。在使用之前,请遵循其readme中指定的配置步骤

在遵循他们的配置步骤后,请确保您已在.env文件中指定了您的FIREBASE_CREDENTIALS

用法

在设置您的Firebase凭证后,您现在可以通过Notification类和通过FcmChannel::class发送来通过FCM发送通知。

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')));
    }
}

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

class User extends Authenticatable
{
    use Notifiable;

    ....

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

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

$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)。有关更多信息,请参阅许可证文件