首页 > 解决方案 > Laravel FCM 通知未发送所有提供的数据

问题描述

我在Flutter App 的 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 \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');
}

标签: laravelfirebase-cloud-messaging

解决方案


正如您在https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messageshttps://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' }, ])


推荐阅读