spondonit / laravel-fcm-notification
Laravel FCM (Firebase Cloud Messaging) 通知通道
v1.0.0
2024-08-30 10:26 UTC
Requires
- php: >=5.6.4
- guzzlehttp/guzzle: ^6.2|^7.0
- illuminate/config: ~5.3|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/notifications: ~5.3|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/queue: ~5.3|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ~5.3|^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- mockery/mockery: ~1.0
- phpunit/phpunit: ~6.0
README
Laravel FCM (Firebase Cloud Messaging) 通知通道
使用此包通过 Laravel 向 Firebase Cloud Messaging 发送推送通知。需要 Laravel 5.5 或更高版本。
此包仅与 Legacy HTTP Server Protocol 兼容
安装
可以通过 Composer 安装此包。
composer require spondonit/laravel-fcm-notification
如果安装的 Laravel 版本小于 5.5,则需添加服务提供者
// config/app.php
'providers' => [
...
SpondonIt\FCM\FcmNotificationServiceProvider::class,
...
];
在 config/services.php 中添加您的 Firebase API 密钥。
return [
...
...
/*
* Add the Firebase API key
*/
'fcm' => [
'key' => env('FCM_SECRET_KEY')
]
];
示例用法
使用 Artisan 创建通知
php artisan make:notification SomeNotification
在您的通知的 public function via($notifiable) 方法中返回 [fcm]
public function via($notifiable)
{
return ['fcm'];
}
在您的通知中添加方法 public function toFcm($notifiable),并返回一个 FcmMessage 实例
use SpondonIt\FCM\FcmMessage;
...
public function toFcm($notifiable)
{
$message = new FcmMessage();
$message->content([
'title' => 'Foo',
'body' => 'Bar',
'sound' => '', // Optional
'icon' => '', // Optional
'click_action' => '' // Optional
])->data([
'param1' => 'baz' // Optional
])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
return $message;
}
在向特定设备发送时,请确保您的通知实体定义了 routeNotificationForFcm 方法
/**
* Route notifications for the FCM channel.
*
* @param \Illuminate\Notifications\Notification $notification
* @return string
*/
public function routeNotificationForFcm($notification)
{
return $this->device_token;
}
在向一个主题发送时,您可以在通知的 toFcm 方法中定义
use SpondonIt\FCM\FcmMessage;
...
public function toFcm($notifiable)
{
$message = new FcmMessage();
$message->to('the-topic', $recipientIsTopic = true)
->content([...])
->data([...]);
return $message;
}
或者当发送带有条件时
use SpondonIt\FCM\FcmMessage;
...
public function toFcm($notifiable)
{
$message = new FcmMessage();
$message->contentAvailable(true)
->priority('high')
->condition("'user_".$notifiable->id."' in topics")
->data([...]);
return $message;
}
您可以使用 setHeaders() 提供可选头或覆盖请求头
use SpondonIt\FCM\FcmMessage;
...
public function toFcm($notifiable)
{
$message = new FcmMessage();
$message->setHeaders([
'project_id' => "48542497347" // FCM sender_id
])->content([
'title' => 'Foo',
'body' => 'Bar',
'sound' => '', // Optional
'icon' => '', // Optional
'click_action' => '' // Optional
])->data([
'param1' => 'baz' // Optional
])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
return $message;
}
解析响应
要处理任何 Laravel 通知通道的响应,请检查 Laravel 通知事件
此通道返回一个 JSON 数组响应
{
"multicast_id": "number",
"success": "number",
"failure": "number",
"canonical_ids": "number",
"results": "array"
}
有关响应解释的文档,请参阅 FCM Legacy HTTP Server Protocol
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件