Yura
Yura

Reputation: 2248

Laravel FCM Notification not sending all provided data

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 \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 (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');
}

Upvotes: 1

Views: 3746

Answers (1)

Iam ByeBlogs
Iam ByeBlogs

Reputation: 753

As you can see on https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages And https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Notification.FIELDS.body

with this : Arbitrary key/value payload. If present, it will override google.firebase.fcm.v1.Message.data.

An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.

you can fill :

->setData([ 'data' => {'foo':'foo' }, ])

Upvotes: 2

Related Questions