Laravel Firebase云消息传递。
v1.0.2
2023-09-06 07:59 UTC
Requires
- php: ^8.1
- illuminate/notifications: ^8.0|^9.0|^10.0
- illuminate/support: ^8.0|^9.0|^10.0
- spatie/laravel-package-tools: ^1.14.1
Requires (Dev)
- laravel/pint: ^1.6
- nunomaduro/collision: ^7.6
- orchestra/testbench: ^8.0.8
- pestphp/pest: ^2.7
- pestphp/pest-plugin-laravel: ^2.0
- phpunit/phpunit: ^10.2.2
This package is auto-updated.
Last update: 2024-09-06 10:28:49 UTC
README
介绍
LaravelFcm 是一个提供通过Firebase在Laravel中发送推送通知或自定义消息的包。
Firebase云消息传递(FCM)是一个跨平台的消息解决方案,允许你免费可靠地传递消息。
对于即时消息等用例,消息可以传输最多4KB的有效负载到客户端应用。
安装
按照以下步骤安装此包。
Composer
composer require smirltech/laravel-fcm
复制配置文件
运行 php artisan vendor:publish --provider="SmirlTech\LaravelFcm\Providers\LaravelFcmServiceProvider" 以发布 laravel-fcm.php 配置文件。
获取认证密钥
从 https://console.firebase.google.com/ 获取认证密钥
根据需要配置laravel-fcm.php
'server_key' => '{FCM_SERVER_KEY}'
使用
按照以下步骤了解如何使用此包。
在 Controller/Service 或任何类中的示例使用
use SmirlTech\LaravelFcm\Facades\LaravelFcm; class MyController { private $deviceTokens =['{TOKEN_1}', '{TOKEN_2}']; public function sendNotification() { return LaravelFcm::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 LaravelFcm::fromArray(['title' => 'Test Title', 'body' => 'Test body'])->sendNotification($this->deviceTokens); } public function sendMessage() { return LaravelFcm::withTitle('Test Title') ->withBody('Test body') ->sendMessage($this->deviceTokens); // Or return LaravelFcm::fromArray(['title' => 'Test Title', 'body' => 'Test body'])->sendMessage($this->deviceTokens); } }
在 Notification 类中的示例使用
use Illuminate\Notifications\Notification; use SmirlTech\LaravelFcm\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客户端中接收消息或推送通知的示例。
- 您可以使用
laravelfcm()助手函数代替Facade。
有效负载
检查如何将有效负载发送到Firebase的构成
示例 1
laravelFcm::withTitle('Test Title')->withBody('Test body')->sendNotification('token1');
{
"registration_ids": [
"token1"
],
"notification": {
"title": "Test Title",
"body": "Test body"
},
"priority": "normal"
}
示例 2
laravelFcm::withTitle('Test Title')->withBody('Test body')->sendMessage('token1');
{
"registration_ids": [
"token1"
],
"data": {
"title": "Test Title",
"body": "Test body"
}
}
如果您想从头开始创建有效负载,可以使用方法 fromRaw,例如
return LaravelFbase::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();
