abdullahfaqeir / fcm
FCM (Firebase Cloud Messaging) 通知驱动程序,适用于 Laravel
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.0.1
- illuminate/notifications: ~8.0 || ~9.0 || ~10.0
- illuminate/support: ~8.0 || ~9.0 || ~10.0
- kreait/laravel-firebase: ^5.0
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ^9.0
README
此包使您能够通过 Laravel 8.x 和 9.x 使用 Firebase Cloud Messaging (FCM) 发送通知变得简单。
免责声明
此仓库是从 原始仓库 分支出来的,但已更新到版本 3,请查看以下说明。
版本 3 发布(2022年3月26日)
版本 3.0.0 已发布,支持 Laravel 9,不再支持低于 Laravel 8 的版本,现在最低 PHP 版本为 8.1,因为该包使用了 PHP 原生枚举。
版本 2 发布(2020年3月4日)
版本 2.0.0 已发布,并将 FCM API 调用从旧版 HTTP 迁移到 HTTP v1(Firebase 文档见此处)。这是一个破坏性变更,因此使用 v1.x 的通知不应升级到本包的 v2.x 版本,除非您计划迁移您的通知类。
内容
安装
使用 Composer 安装此包
composer require abdullahfaqeir/fcm:~3.0
设置 FCM 服务
此包现在使用 laravel-firebase 库进行身份验证并对 Firebase 进行 API 调用。在使用此包之前,请按照他们自述文件中指定的配置步骤进行操作。
按照他们的配置步骤进行后,请确保您已在您的 .env 文件中指定了您的 FIREBASE_CREDENTIALS
。
使用
设置好 Firebase 凭据后,您现在可以通过 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)
变更日志
有关最近更改的更多信息,请参阅变更日志。
测试
$ composer test
安全
如果您发现任何安全相关的问题,请通过电子邮件 chrisbjr@gmail.com 而不是使用问题跟踪器。
贡献
有关详细信息,请参阅 贡献。
致谢
许可
MIT 许可证(MIT)。请参阅 许可文件 获取更多信息。