akshyaraait / laravel-fcm-notification
Laravel FCM (Firebase Cloud Messaging) 通知通道
dev-master
2023-12-28 12:03 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
This package is auto-updated.
Last update: 2024-09-28 13:30:49 UTC
README
Laravel FCM (Firebase Cloud Messaging) 通知通道
使用此包通过 Laravel 向 Firebase Cloud Messaging 发送推送通知。需要 Laravel 5.5+。
此包仅与 旧版 HTTP 服务器协议 兼容
安装
此包可以通过 Composer 安装。
composer require akshyaraait/laravel-fcm-notification
如果是在 < Laravel 5.5 上安装,则需要添加服务提供者
// config/app.php 'providers' => [ ... AkshyaraaIt\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 AkshyaraaIt\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 AkshyaraaIt\FCM\FcmMessage; ... public function toFcm($notifiable) { $message = new FcmMessage(); $message->to('the-topic', $recipientIsTopic = true) ->content([...]) ->data([...]); return $message; }
或者当发送条件满足时
use AkshyaraaIt\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 AkshyaraaIt\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)。有关更多信息,请参阅 许可证文件。