gem-partij / gemboot-larafirebase
Laravel Firebase云消息服务。
2.0.0
2024-05-06 07:04 UTC
Requires
- illuminate/notifications: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
This package is auto-updated.
Last update: 2024-09-06 07:46:59 UTC
README
本包是从 kutia-software-company/larafirebase 分支出来的。
简介
Larafirebase 是一个Laravel扩展包,允许您通过Firebase发送推送通知或自定义消息。
Firebase云消息服务(FCM)是一个跨平台的消息解决方案,它允许您免费可靠地发送消息。
对于即时消息等用例,消息可以传输高达4KB的有效负载到客户端应用程序。
安装
按照以下步骤安装此包。
Composer
composer require kutia-software-company/larafirebase
复制配置
运行 php artisan vendor:publish --provider="Kutia\Larafirebase\Providers\LarafirebaseServiceProvider"
以发布 larafirebase.php
配置文件。
获取认证密钥
从 https://console.firebase.google.com/ 获取认证密钥。
根据需要配置 larafirebase.php
'authentication_key' => '{AUTHENTICATION_KEY}'
用法
按照以下步骤了解如何使用此包。
在 Controller/Service 或任何类中的示例用法
use Kutia\Larafirebase\Facades\Larafirebase; class MyController { private $deviceTokens =['{TOKEN_1}', '{TOKEN_2}']; public function sendNotification() { return Larafirebase::withTitle('Test Title') ->withBody('Test body') ->withImage('https://firebase.google.com/images/social.png') ->withIcon('https://seeklogo.com/images/F/firebase-logo-402F407EE0-seeklogo.com.png') ->withSound('default') ->withClickAction('https://www.google.com') ->withPriority('high') ->withAdditionalData([ 'color' => '#rrggbb', 'badge' => 0, ]) ->sendNotification($this->deviceTokens); // Or return Larafirebase::fromArray(['title' => 'Test Title', 'body' => 'Test body'])->sendNotification($this->deviceTokens); } public function sendMessage() { return Larafirebase::withTitle('Test Title') ->withBody('Test body') ->sendMessage($this->deviceTokens); // Or return Larafirebase::fromArray(['title' => 'Test Title', 'body' => 'Test body'])->sendMessage($this->deviceTokens); } }
在 Notification 类中的示例用法
use Illuminate\Notifications\Notification; use Kutia\Larafirebase\Messages\FirebaseMessage; class SendBirthdayReminder extends Notification { /** * Get the notification's delivery channels. */ public function via($notifiable) { return ['firebase']; } /** * Get the firebase representation of the notification. */ public function toFirebase($notifiable) { $deviceTokens = [ '{TOKEN_1}', '{TOKEN_2}' ]; return (new FirebaseMessage) ->withTitle('Hey, ', $notifiable->first_name) ->withBody('Happy Birthday!') ->asNotification($deviceTokens); // OR ->asMessage($deviceTokens); } }
技巧
- 查看如何在 JavaScript客户端 中接收消息或推送通知的示例。
- 您可以使用
larafirebase()
辅助函数而不是门面。
有效负载
检查发送到Firebase的有效负载是如何形成的。
示例 1
Larafirebase::withTitle('Test Title')->withBody('Test body')->sendNotification('token1');
{ "registration_ids": ["token1"], "notification": { "title": "Test Title", "body": "Test body" }, "priority": "normal" }
示例 2
Larafirebase::withTitle('Test Title')->withBody('Test body')->sendMessage('token1');
{ "registration_ids": ["token1"], "data": { "title": "Test Title", "body": "Test body" } }
如果您要从头创建有效负载,可以使用 fromRaw
方法,例如
return Larafirebase::fromRaw([ 'registration_ids' => ['token1', 'token2'], 'data' => [ 'key_1' => 'Value 1', 'key_2' => 'Value 2' ], 'android' => [ 'ttl' => '1000s', 'priority' => 'normal', 'notification' => [ 'key_1' => 'Value 1', 'key_2' => 'Value 2' ], ], ])->send();
由 Gentrit Abazi(@gentritabazi)用爱心制作。