Laravel FCM Notification not sending all provided data

  firebase-cloud-messaging, laravel

I created a notification via Firebase Cloud Messaging (FCM) using @laravel-notification-channels/fcm package in my Laravel app for Flutter App. It works, but there is some issue, I was expecting to get all the data given from this line:

->setData([
    'agenda' => $this->agenda,
    'data2' => 'hello world!',
    'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
])

But instead, on the client side, It only gave me the click_action object.

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): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Why is this happening? Did I do any mistake? Please have a look at full implementation below:

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 (In case you are wondering the problem is on the logging script)

static Future _onMessage(Map<String, dynamic> message) async {
  _logger.logNST.d(message, 'NotificationService: onMessage');
}

Source: Laravel

Leave a Reply