项目作者: plokko

项目描述 :
Laravel Firebase API implementation
高级语言: PHP
项目地址: git://github.com/plokko/laravel-firebase.git
创建时间: 2018-02-21T22:46:03Z
项目社区:https://github.com/plokko/laravel-firebase

开源协议:MIT License

下载


Laravel Firebase

Build Status
Packagist
Packagist
Packagist

Laravel Firebase integration

This package includes:

  • Firebase OAuthV2.0 authentication, with token caching
  • Centralized ServiceAccount credential management
  • Firebase FCM Http V1 API and Firebase Realtime database REST api via OAuth authentication
  • Firebase JWT token generator (via php-jwt)
  • Automatic sync for Eloquent models to Firebase Realtime db
  • Automatic sync triggers on related model changes

Installation

Install via composer

  1. composer require plokko/laravel-firebase

The package will be auto registered in laravel >=5.5;
If you use laravel <5.5 follow the next two steps

  1. Add service provider to config/app.php in providers section

    1. Plokko\LaravelFirebase\ServiceProvider::class,
  2. Register package facade in config/app.php in aliases section

    1. Plokko\LaravelFirebase\Facades\LaravelFirebase::class,

    Your file in config/laravel-firebase.php should now look like this:
    ```php
    <?php

return [
‘read_only’ => env(‘FIREBASEDB_READONLY’,false),//DEBUG

  1. /**
  2. * Firebase service account information, can be either:
  3. * - string : absolute path to serviceaccount json file
  4. * - string : content of serviceaccount (json string)
  5. * - array : php array conversion of the serviceaccount
  6. * @var array|string
  7. */
  8. 'service_account' => base_path('.firebase-credentials.json'),
  9. /**
  10. * If set to true will enable Google OAuth2.0 token cache storage
  11. */
  12. 'cache' => true,
  13. /**
  14. * Cache driver for OAuth token cache,
  15. * if null default cache driver will be used
  16. * @var string|null
  17. */
  18. 'cache_driver' => null,
  19. /**
  20. * Specify if and what event to trigger if an invalid token is returned
  21. * @var string|null
  22. */
  23. 'FCMInvalidTokenTriggerEvent' => null,

];

  1. ### Configuration
  2. Publish the configuration file via
  3. ```bash
  4. php artisan vendor:publish --provider="Plokko\LaravelFirebase\ServiceProvider" --tag="config"

Usage

JWT token

You can easly create a Firebase JWT token (for auth) with FirebaseJWT::encode:

  1. FirebaseJWT::encode($uid,['optional'=>'custom-claims-array']);

FCM

This package allows you to send FCM messages via FCM http v1 api

Message builder

You can easly build FCM Messages via the FCM facade:

  1. FCM::notificationTitle('My notification title')
  2. ->notificationBody('my notification body...');
  3. ->data(['notification' => 'data'])
  4. ->highPriority()//note: not all devices may use all the fields like priority or ttl
  5. ->ttl('20.5s')
  6. ->toDevice('my-device-fcm-token') // or toTopic('topic-name') or toCondition('condition-name') or toTarget(Target)
  7. ->send();//Submits the message

FCM Notification channel

You can also send the FCM messages via the FcmNotificationChannel channel:

  1. class TestFcmNotification extends Notification implements ShouldQueue
  2. {
  3. use Queueable;
  4. /**
  5. * Get the notification's delivery channels.
  6. *
  7. * @param mixed $notifiable
  8. * @return array
  9. */
  10. public function via($notifiable)
  11. {
  12. return [FcmNotificationChannel::class];
  13. }
  14. public function toFcm($notifiable)
  15. {
  16. return FCM::notificationTitle('Test notification')
  17. ->notificationBody('notification body...')
  18. ->toDevice($notifiable->deviceToken);
  19. }
  20. }

Real time database

Settings:

You can enable read-only access to database setting

  1. FIREBASEDB_READONLY=true

on your .env file, this is usefull for testing purpuses, the writes will not return any error but will not be executed on Firebase.

Query the Realtime database

To get an instance of the database use the FirebaseDb facade:

  1. $test = FirebaseDb::getReference('test'); //get the reference for item /test
  2. $test->get('01');//Get /test/01 as an array
  3. $test01 = $test->getReference('01');//Get a reference for /test/01
  4. $test01->set('label','value');//Set /test/01/label = value

Sync models to Firebase

see Firebase database sync