avto-dev/firebase-notifications-laravel
Firebase服务通知通道
v2.8.0
2024-05-31 11:26 UTC
Requires
- php: ^8.1
- ext-json: *
- ext-mbstring: *
- google/apiclient: ^2.2
- guzzlehttp/guzzle: ~7.5
- illuminate/config: ~10.0 || ~11.0
- illuminate/contracts: ~10.0 || ~11.0
- illuminate/notifications: ~10.0 || ~11.0
- illuminate/support: ~10.0 || ~11.0
Requires (Dev)
- laravel/laravel: ~10.0 || ~11.0
- mockery/mockery: ^1.6.5
- phpstan/phpstan: ^1.10.66
- phpunit/phpunit: ^10.5
README
这里是Laravel通知系统的最新文档:https://laravel.net.cn/docs/master/notifications
Laravel应用的Firebase通知通道
此包使得使用Firebase在Laravel 5中发送通知变得简单。
内容
安装
$ composer require avto-dev/firebase-notifications-laravel "^2.0"
需要安装
composer
(如何安装composer)。
您需要修复包的主版本。
Laravel 5.5及以上版本使用包自动发现,因此不需要您手动注册服务提供者。否则,您必须将服务提供者添加到./config/app.php
中的providers
数组。
<?php return [ // ... 'providers' => [ // ... AvtoDev\FirebaseNotificationsChannel\ServiceProvider::class, ], ];
设置Firebase服务
您需要在配置文件./config/services.php
中设置Firebase通道。
为您的服务账户生成私有密钥文件
- 在Firebase控制台中,打开设置 > 服务账户。
- 点击生成新私有密钥,然后通过点击生成密钥进行确认。
- 安全地存储包含密钥的JSON文件。您将需要此JSON文件以完成下一步。
接下来选择“driver” file
或config
,其中包含在./config/services.php
中Firebase服务账户的凭据
<?php return [ // ... /* |-------------------------------------------------------------------------- | Firebase Settings section |-------------------------------------------------------------------------- | | Here you may specify some configs for FCM. | */ 'fcm' => [ /* |---------------------------------------------------------------------- | Firebase service driver |---------------------------------------------------------------------- | | Value `file` or `config`: | - Select `file` option to make service read json file | - Select `config` option to set up all section in config file | */ 'driver' => env('FCM_DRIVER', 'config'), /* |--------------------------------------------------------------------- | FCM Drivers |--------------------------------------------------------------------- | | Here are each of the firebase. | */ 'drivers' => [ 'file' => [ 'path' => env('FCM_FILE_PATH', base_path('storage/fcm.json')), ], 'config' => [ /* |------------------------------------------------------------ | Credentials |------------------------------------------------------------ | | Content of `firebase.json` file in config. Using if | `fcm.driver` is `config`. All fields required! | */ 'credentials'=>[ 'private_key_id' => env('FCM_CREDENTIALS_PRIVATE_KEY_ID', 'da80b3bbceaa554442ad67e6be361a66'), 'private_key' => env('FCM_CREDENTIALS_PRIVATE_KEY', '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n'), 'client_email' => env('FCM_CREDENTIALS_CLIENT_EMAIL', 'firebase-adminsdk-mwax6@test.iam.gserviceaccount.com'), 'client_id' => env('FCM_CREDENTIALS_CLIENT_ID', '22021520333507180281'), 'auth_uri' => env('FCM_CREDENTIALS_AUTH_URI', 'https://#/o/oauth2/auth'), 'token_uri' => env('FCM_CREDENTIALS_TOKEN_URI', 'https://oauth2.googleapis.com/token'), 'auth_provider_x509_cert_url' => env('FCM_CREDENTIALS_AUTH_PROVIDER_CERT', 'https://www.googleapis.com/oauth2/v1/certs'), 'client_x509_cert_url' => env('FCM_CREDENTIALS_CLIENT_CERT', 'https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-mwax6%40test.iam.gserviceaccount.com'), ], ], ], ], ];
用法
现在您可以在通知内的via()
方法中使用该通道,以及发送推送通知
<?php use Illuminate\Notifications\Notification; use AvtoDev\FirebaseNotificationsChannel\FcmChannel; use AvtoDev\FirebaseNotificationsChannel\FcmMessage; class AccountApproved extends Notification { public function via($notifiable) { return [FcmChannel::class]; } public function toFcm($notifiable, $notification) { return (new FcmMessage) ->setTitle('Approved!') ->setBody('Your account was approved!'); } }
<?php use Illuminate\Notifications\Notifiable; use AvtoDev\FirebaseNotificationsChannel\Receivers\FcmDeviceReceiver; use AvtoDev\FirebaseNotificationsChannel\Receivers\FcmNotificationReceiverInterface; class SomeNotifible { use Notifiable; /** * Reveiver of firebase notification. * * @return FcmNotificationReceiverInterface */ public function routeNotificationForFcm(): FcmNotificationReceiverInterface { return new FcmDeviceReceiver('firebase_token'); } }
可用的消息方法
此包支持HTTP v1 FCM API的所有字段。消息类包含所有字段的setter
PlatformSettings类包含平台特定的setter
测试
为了包测试,我们使用phpunit
框架。只需在您的终端中输入(需要安装docker-ce
)
$ git clone git@github.com:avto-dev/firebase-notifications-laravel.git ./firebase-notifications-laravel && cd $_ $ make install $ make test
变更日志
变更日志可以在此处找到。
支持
如果您发现任何包错误,请在当前仓库中创建一个问题。
安全
如果您发现任何安全相关的问题,请通过电子邮件发送到jetexe2@gmail.com
而不是使用问题跟踪器。
致谢
许可协议
这是一个开源软件,根据MIT许可协议授权。