Laravel FCM 通知未发送所有提供的数据
Laravel FCM Notification not sending all provided data
我在我的 Laravel Flutter App 应用程序中使用 @laravel-notification-channels/fcm 包通过 Firebase Cloud Messaging (FCM) 创建了一个通知。它有效,但有一些问题,我期待从这一行获得所有数据:
->setData([
'agenda' => $this->agenda,
'data2' => 'hello world!',
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
])
但是,在客户端,它只给了我 click_action
对象。
I/flutter (31507): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (31507): │ NotificationService: onMessage
I/flutter (31507): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
I/flutter (31507): │ {
I/flutter (31507): │ "notification": {
I/flutter (31507): │ "title": "Agenda Invitation",
I/flutter (31507): │ "body": "You've been invited to participate in Wirda's agenda."
I/flutter (31507): │ },
I/flutter (31507): │ "data": {
I/flutter (31507): │ "click_action": "FLUTTER_NOTIFICATION_CLICK"
I/flutter (31507): │ }
I/flutter (31507): │ }
I/flutter (31507): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
为什么会这样?我做错了吗?请查看下面的完整实施:
AgendaCreated.php
class AgendaCreated extends Notification implements ShouldQueue
{
use Queueable;
public $agenda;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Agenda $agenda)
{
$this->agenda = $agenda;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [FcmChannel::class];
}
/**
* Get the fcm representation of the notification.
*
* @param mixed $notifiable
* @return \NotificationChannels\Fcm\FcmMessage
*/
public function toFcm($notifiable)
{
$trimmedAuthorsName = Str::words($this->agenda->author->name, 1, '');
$author = Str::ucfirst($trimmedAuthorsName);
$notification = ResourcesNotification::create()
->setTitle('Agenda Invitation')
->setBody('You\'ve been invited to participate in ' . $author . '\'s agenda.');
$androidConfig = AndroidConfig::create()
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
->setNotification(AndroidNotification::create()->setColor('#0A0A0A'));
$iosConfig = ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'));
return FcmMessage::create()
->setTopic('agenda')
->setData([
'agenda' => $this->agenda,
'data2' => 'hello world!',
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
])
->setNotification($notification)
->setAndroid($androidConfig)
->setApns($iosConfig);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
NotificationService.dart
(如果您想知道问题出在日志记录脚本上)
static Future _onMessage(Map<String, dynamic> message) async {
_logger.logNST.d(message, 'NotificationService: onMessage');
}
如您所见https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
和
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Notification.FIELDS.body
有了这个:
任意 key/value 负载。如果存在,它将覆盖 google.firebase.fcm.v1.Message.data.
包含“键”列表的对象:值对。例子:
{ "name": "wrench", "mass": "1.3kg", "count": "3" }
.
你可以填写:
->setData([ 'data' => {'foo':'foo' }, ])
我在我的 Laravel Flutter App 应用程序中使用 @laravel-notification-channels/fcm 包通过 Firebase Cloud Messaging (FCM) 创建了一个通知。它有效,但有一些问题,我期待从这一行获得所有数据:
->setData([
'agenda' => $this->agenda,
'data2' => 'hello world!',
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
])
但是,在客户端,它只给了我 click_action
对象。
I/flutter (31507): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (31507): │ NotificationService: onMessage
I/flutter (31507): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
I/flutter (31507): │ {
I/flutter (31507): │ "notification": {
I/flutter (31507): │ "title": "Agenda Invitation",
I/flutter (31507): │ "body": "You've been invited to participate in Wirda's agenda."
I/flutter (31507): │ },
I/flutter (31507): │ "data": {
I/flutter (31507): │ "click_action": "FLUTTER_NOTIFICATION_CLICK"
I/flutter (31507): │ }
I/flutter (31507): │ }
I/flutter (31507): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
为什么会这样?我做错了吗?请查看下面的完整实施:
AgendaCreated.php
class AgendaCreated extends Notification implements ShouldQueue
{
use Queueable;
public $agenda;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Agenda $agenda)
{
$this->agenda = $agenda;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [FcmChannel::class];
}
/**
* Get the fcm representation of the notification.
*
* @param mixed $notifiable
* @return \NotificationChannels\Fcm\FcmMessage
*/
public function toFcm($notifiable)
{
$trimmedAuthorsName = Str::words($this->agenda->author->name, 1, '');
$author = Str::ucfirst($trimmedAuthorsName);
$notification = ResourcesNotification::create()
->setTitle('Agenda Invitation')
->setBody('You\'ve been invited to participate in ' . $author . '\'s agenda.');
$androidConfig = AndroidConfig::create()
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
->setNotification(AndroidNotification::create()->setColor('#0A0A0A'));
$iosConfig = ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'));
return FcmMessage::create()
->setTopic('agenda')
->setData([
'agenda' => $this->agenda,
'data2' => 'hello world!',
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
])
->setNotification($notification)
->setAndroid($androidConfig)
->setApns($iosConfig);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
NotificationService.dart
(如果您想知道问题出在日志记录脚本上)
static Future _onMessage(Map<String, dynamic> message) async {
_logger.logNST.d(message, 'NotificationService: onMessage');
}
如您所见https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages 和 https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Notification.FIELDS.body
有了这个: 任意 key/value 负载。如果存在,它将覆盖 google.firebase.fcm.v1.Message.data.
包含“键”列表的对象:值对。例子:
{ "name": "wrench", "mass": "1.3kg", "count": "3" }
.
你可以填写:
->setData([ 'data' => {'foo':'foo' }, ])