amos97/laravel-firebase

v8.0.0 2021-07-28 15:45 UTC

This package is auto-updated.

Last update: 2024-09-28 22:41:36 UTC


README

Build Status Packagist Packagist Packagist

Laravel Firebase 集成

此包包含

  • Firebase OAuthV2.0 身份验证,带有令牌缓存
  • 集中式 ServiceAccount 凭证管理
  • 通过 OAuth 认证使用 Firebase FCM Http V1 API 和 Firebase 实时数据库 REST API
  • Firebase JWT 令牌生成器(通过 php-jwt)
  • Eloquent 模型与 Firebase 实时数据库自动同步
  • 在相关模型更改时自动触发同步

安装

通过 composer 安装

composer require amos97/laravel-firebase

该包将在 laravel >=5.5 中自动注册;如果您使用 laravel <5.5,请按照以下两个步骤操作

  1. 将服务提供者添加到 config/app.php 文件中的 providers 部分
Plokko\LaravelFirebase\ServiceProvider::class,
  1. 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

将模型同步到 Firebase

请参阅 Firebase 数据库同步