bkief29 / fcm
FCM (Firebase Cloud Messaging) 通知驱动程序,适用于 Laravel 5.5+,6.x,7.x 和 8.x。
Requires
- php: >=7.1.3
- guzzlehttp/guzzle: ^6.2 || ^7.0
- kreait/laravel-firebase: ^1.3 || ^2.1 || ^3.0
- spatie/enum: ^2.3 || ^3.0
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: 8.*
README
此包使用 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。在开始使用之前,请遵循其 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'))); } // 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)。有关更多信息,请参阅许可文件。