agrodata / laravel-api-gateway
Laravel (AwsApiGateway) 通知频道
此包的官方仓库似乎已不存在,因此该包已被冻结。
v0.1
2022-02-28 20:24 UTC
Requires
- php: >=7.3
- guzzlehttp/guzzle: ^6.2|^7.0
- illuminate/config: ~5.3|^6.0|^7.0|^8.0
- illuminate/notifications: ~5.3|^6.0|^7.0|^8.0
- illuminate/queue: ~5.3|^6.0|^7.0|^8.0
- illuminate/support: ~5.3|^6.0|^7.0|^8.0
Requires (Dev)
- mockery/mockery: ~1.0
- phpunit/phpunit: ~6.0
This package is auto-updated.
Last update: 2022-03-03 14:22:47 UTC
README
Laravel FCM (Firebase Cloud Messaging) 通知频道
使用此包通过Laravel向Firebase Cloud Messaging发送推送通知。需要Laravel 5.5+。
此包仅支持旧版HTTP服务器协议
安装
此包可以通过Composer安装。
composer require benwilkins/laravel-fcm-notification
如果要在Laravel 5.5以下版本上安装,则需要添加服务提供者
// config/app.php
'providers' => [
...
Benwilkins\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 Benwilkins\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 Benwilkins\FCM\FcmMessage;
...
public function toFcm($notifiable)
{
$message = new FcmMessage();
$message->to('the-topic', $recipientIsTopic = true)
->content([...])
->data([...]);
return $message;
}
或者当在条件满足时发送
use Benwilkins\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 Benwilkins\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旧版HTTP服务器协议
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件