feugene / firebase-notifications-laravel
为Laravel提供的Firebase通知
v2.1.2
2019-05-24 10:46 UTC
Requires
- php: >=7.2
- ext-json: *
- ext-mbstring: *
- efureev/support: 2.0.*
- google/apiclient: ^2.2
- guzzlehttp/guzzle: ~6.3
- illuminate/notifications: >=5.8.0 < 5.9.0
- illuminate/support: >=5.8.0 <5.9.0
Requires (Dev)
- feugene/dev-tools: ^2.0
- laravel/laravel: >=5.8.0 <5.9.0
- mockery/mockery: ~1.2
- phpstan/phpstan: ^0.11
- phpunit/phpunit: ^8.0
- symfony/var-dumper: ~3.2 || ^4.0
README
以下是关于Laravel通知系统的最新文档:https://laravel.net.cn/docs/master/notifications
FirebaseNotificationsChannel用于Laravel应用
此包使您能够通过Laravel 5轻松发送Firebase通知。
内容
安装
$ composer require feugene/firebase-notifications-laravel "^2.1"
需要安装
composer
(如何安装composer)。
您需要修复包的主版本号。
Laravel 5.5及以上版本使用包自动发现,因此不需要您手动注册服务提供程序。否则,您必须将服务提供程序添加到 ./config/app.php
中的 providers
数组。
<?php return [ // ... 'providers' => [ // ... Feugene\FirebaseNotificationsChannel\FcmServiceProvider::class, ], ];
如果您想禁用包服务提供程序的自动发现,只需将以下行添加到您的 composer.json 中
{ "extra": { "laravel": { "dont-discover": [ "feugene/firebase-notifications-laravel" ] } } }
设置Firebase服务
您需要在配置文件 ./config/services.php
中设置firebase通道。
为您的服务账户生成私钥文件
- 在Firebase控制台中,打开 设置 > 服务账户。
- 点击 生成新私钥,然后通过点击 生成密钥 来确认。
- 安全地存储包含密钥的JSON文件。您将需要此JSON文件来完成下一步。
接下来,选择“driver” file
或 config
,它包含 Firebase服务账户 的凭据在 ./config/services.php
中
<?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 Feugene\FirebaseNotificationsChannel\FcmChannel; use Feugene\FirebaseNotificationsChannel\FcmMessage; class AccountApproved extends Notification { public function via($notifiable) { return [FcmChannel::class]; } /** * @return FcmMessage */ public function toFcm(): FcmMessage { return (new FcmMessage) ->setTitle('Approved!') ->setBody('Your account was approved!'); } }
<?php use Illuminate\Notifications\Notifiable; use Feugene\FirebaseNotificationsChannel\Receivers\FcmDeviceReceiver; use Feugene\FirebaseNotificationsChannel\Receivers\FcmNotificationReceiverInterface; class SomeNotifible { use Notifiable; /** * Reveiver of firebase notification. * * @return FcmNotificationReceiverInterface */ public function routeNotificationForFcm(): FcmNotificationReceiverInterface { return new FcmDeviceReceiver($this->firebase_token); } }
订阅主题上的设备
<?php class PushTopicSendCommand extends Command { public function handle(): void { $topicName = 'topic-name'; $resultList = app('firebase')->subscribe($topicName, [ "eKYl5MT4Ra8:APA91bHZXfYo....tGMs4UxJS9LI_V", "eKY...UxJS9LI_F", ]); dd($resultList, app('firebase')->getLastResponse()); } }
示例通知消息
<?php namespace App\Notifications; use Feugene\FirebaseNotificationsChannel\FcmChannel; use Feugene\FirebaseNotificationsChannel\FcmMessage; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Notifications\Notification; /** * Class PushToDeviceMsg * @package App\Notifications */ class PushToDeviceMsg extends Notification implements Arrayable { /** @var string */ protected $title; /** @var string */ protected $body; /** @var array */ protected $data; /** @var bool */ protected $mutableContent; public function __construct(string $title, string $body, array $data = [], $mutableContent = true) { $this->title = $title; $this->body = $body; $this->data = $data; $this->mutableContent = $mutableContent; } /** * @return array */ public function via(): array { return [FcmChannel::class]; } /** * @return FcmMessage */ public function toFcm(): FcmMessage { $msg = (new FcmMessage) ->setTitle($this->title) ->setBody($this->body) ->setData($this->data); if ($this->mutableContent) { $msg ->getApns() ->enableMutableContent(); $msg ->getApns() ->setCategory('ActionRichPush'); $msg ->getApns() ->setTitle($this->title); $msg ->getApns() ->setBody($this->body); $msg ->getAndroid() ->setHideNotification(true); } return $msg; } /** * @return array */ public function toArray(): array { return [ 'title' => $this->title, 'body' => $this->body, 'data' => $this->data, ]; } }
可用的消息方法
此包支持HTTP v1 FCM API中所有字段。消息类包含所有字段的setter
PlatformSettings 类包含平台特定的setter
测试
对于包测试,我们使用 phpunit
框架。只需在您的终端中写入 (需要安装 docker-ce
)
$ git clone git@github.com:efureev/firebase-notifications-laravel.git ./firebase-notifications-laravel && cd $_ $ make install $ make test
变更日志
变更日志可以在这里找到。
支持
如果您发现任何包错误,请在此存储库中创建问题。
安全性
如果您发现任何安全相关的问题,请发送电子邮件至 jetexe2@gmail.com
而不是使用问题跟踪器。
致谢
许可证
这是一个开源软件,许可协议为MIT许可证。