liliom / laravel-firebase
Laravel FCM (Firebase Cloud Messaging) 通知通道
v1.0.1
2018-02-26 13:20 UTC
Requires
- php: >=5.5.9
- guzzlehttp/guzzle: ~6.0
- illuminate/support: 5.*
This package is auto-updated.
Last update: 2024-08-26 11:41:02 UTC
README
此包通过 Laravel 通知通道轻松发送使用 Firebase Cloud Messaging (FCM) 的通知。
安装
此包可以通过 Composer 安装。
composer require liliom/laravel-firebase
如果您不使用 Laravel 5.5+,您必须手动添加服务提供者
// config/app.php 'providers' => [ ... Liliom\Firebase\FirebaseServiceProvider::class, ... ];
现在在 config/services.php
中添加您的 Firebase API 密钥。
return [ .... 'firebase' => [ 'key' => '' ], .... ];
使用方法
让我们使用 artisan 命令创建一个通知
php artisan make:notification FirebaseNotification
现在您可以在您的 view()
方法中使用 firebase
通道。
public function via($notifiable) { return ['firebase']; }
在您的通知类中添加一个公共方法 toFirebase($notifiable)
,并返回一个 FirebaseMessage
实例
public function toFirebase($notifiable) { return (new \Liliom\Firebase\FirebaseMessage) ->notification([ 'title' => 'Notification title', 'body' => 'Notification body', 'sound' => '', // Optional 'icon' => '', // Optional 'click_action' => '' // Optional ]) ->setData([ 'param' => 'zxy' // Optional ]) ->setPriority('high'); // Default is 'normal' }
可用方法
setData
: 设置data
。setPriority
: 设置priority
。setTimeToLive
: 设置time_to_live
。setCollapseKey
: 设置collapse_key
。setNotification
: 设置notification
。setCondition
: 设置condition
。setContentAvailable
: 设置content_available
。setMutableContent
: 设置mutable_content
。setPackageName
: 设置restricted_package_name
。
当向特定设备发送时,确保您的通知实体已定义 routeNotificationForFirebase
方法
注意:您可以通过返回一个令牌数组向许多设备发送。
/** * Route notifications for Firebase channel. * * @return string|array */ public function routeNotificationForFirebase() { return $this->device_tokens; }
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件。