plokko / laravel-firebase
Laravel Firebase 集成
0.3.1
2023-11-26 22:45 UTC
Requires
- php: >=7.1
- illuminate/support: >=5.4.0
- plokko/firebase-php: ^0.3.0
Requires (Dev)
- orchestra/testbench: ~3.6.0
- phpunit/phpunit: ~7.0
README
Laravel Firebase 集成
此包包括
- Firebase OAuthV2.0 身份验证,带有令牌缓存
- 集中式 ServiceAccount 凭证管理
- 通过 OAuth 身份验证使用 Firebase FCM Http V1 API 和 Firebase 实时数据库 REST API
- Firebase JWT 令牌生成器(通过 php-jwt)
- 自动同步 Eloquent 模型到 Firebase 实时数据库
- 相关模型变化时自动触发同步
安装
通过 composer 安装
composer require plokko/laravel-firebase
该包将在 laravel >=5.5 中自动注册;如果您使用 laravel <5.5,请遵循以下两个步骤
- 将服务提供者添加到
config/app.php
中的providers
部分
Plokko\LaravelFirebase\ServiceProvider::class,
- 在
config/app.php
中的aliases
部分注册包外观
Plokko\LaravelFirebase\Facades\LaravelFirebase::class,
您在 config/laravel-firebase.php
中的文件现在应该看起来像这样
<?php return [ 'read_only' => env('FIREBASEDB_READONLY',false),//DEBUG /** * Firebase service account information, can be either: * - string : absolute path to serviceaccount json file * - string : content of serviceaccount (json string) * - array : php array conversion of the serviceaccount * @var array|string */ 'service_account' => base_path('.firebase-credentials.json'), /** * If set to true will enable Google OAuth2.0 token cache storage */ 'cache' => true, /** * Cache driver for OAuth token cache, * if null default cache driver will be used * @var string|null */ 'cache_driver' => null, /** * Specify if and what event to trigger if an invalid token is returned * @var string|null */ 'FCMInvalidTokenTriggerEvent' => null, ];
配置
通过
php artisan vendor:publish --provider="Plokko\LaravelFirebase\ServiceProvider" --tag="config"
用法
JWT 令牌
您可以使用 FirebaseJWT::encode
容易地创建 Firebase JWT 令牌(用于身份验证)
FirebaseJWT::encode($uid,['optional'=>'custom-claims-array']);
FCM
此包允许您通过 FCM http v1 API 发送 FCM 消息
消息构建器
您可以通过 FCM
外观轻松构建 FCM 消息
FCM::notificationTitle('My notification title') ->notificationBody('my notification body...'); ->data(['notification' => 'data']) ->highPriority()//note: not all devices may use all the fields like priority or ttl ->ttl('20.5s') ->toDevice('my-device-fcm-token') // or toTopic('topic-name') or toCondition('condition-name') or toTarget(Target) ->send();//Submits the message
FCM 通知通道
您还可以通过 FcmNotificationChannel
通道发送 FCM 消息
class TestFcmNotification extends Notification implements ShouldQueue { use Queueable; /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return [FcmNotificationChannel::class]; } public function toFcm($notifiable) { return FCM::notificationTitle('Test notification') ->notificationBody('notification body...') ->toDevice($notifiable->deviceToken); } }
实时数据库
设置
您可以在 .env
文件上启用对数据库设置的只读访问
FIREBASEDB_READONLY=true
这对于测试目的很有用,写入将不会返回任何错误,但不会在 Firebase 上执行。
查询实时数据库
要获取数据库实例,请使用 FirebaseDb
外观
$test = FirebaseDb::getReference('test'); //get the reference for item /test $test->get('01');//Get /test/01 as an array $test01 = $test->getReference('01');//Get a reference for /test/01 $test01->set('label','value');//Set /test/01/label = value