我在Flutter应用程序的laravel应用程序中使用@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 NotificationChannelsFcmFcmMessage
*/
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
这样:任意键/值负载。如果存在,它将覆盖google.firebase.fcm.v1.Message.data.
一个包含";键":值对。示例:{ "name": "wrench", "mass": "1.3kg", "count": "3" }
。
你可以填写:
->setData([ 'data' => {'foo':'foo' }, ])