kutia-software-company / larafirebase
Laravel Firebase Cloud Messaging。
1.3.8
2023-11-05 21:01 UTC
Requires
- illuminate/notifications: ^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0
README
简介
Larafirebase 是一个包,它允许您通过 Firebase 在 Laravel 中发送推送通知或自定义消息。
Firebase Cloud Messaging (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()
辅助函数而不是 Facade。
有效负载
检查如何构建发送到 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)用爱心制作。