jamespj / multidomain-fcm
FCM (Firebase Cloud Messaging) 通知驱动程序,用于Laravel
3.2.1
2023-05-31 00:39 UTC
Requires
- php: >= 8.1
- guzzlehttp/guzzle: ^7.0
- illuminate/notifications: ^9.0|^10.0
- illuminate/support: ^9.0|^10.0
- kreait/laravel-firebase: ^5.0
Requires (Dev)
- mockery/mockery: ^1.5.1
- phpunit/phpunit: ^10.0
README
此包使用Firebase Cloud Messaging (FCM)简化了使用Laravel发送通知的过程。
内容
安装
使用Composer安装此包
composer require laravel-notification-channels/fcm
设置Fcm服务
此包现在使用laravel-firebase库来验证身份并调用Firebase API。在使用之前,请按照其readme中指定的配置步骤进行操作。
在遵循他们的配置步骤后,请确保您已在.env文件中指定了您的
使用方法
在设置您的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)。有关更多信息,请参阅许可证文件。