1

I have a Laravel application send push notification through firebase cloud messaging using a package called laravel-fcm-notification.

everything is working fine from the Laravel app when a message is sent I got a response like this

{
    "multicast_id" => 524702081778807088
    "success" => 1
    "failure" => 0
    "canonical_ids" => 0

}

package code

public function toFcm($notifiable)
    {
        $message = new FcmMessage();
        $message->setHeaders([
            'project_id' => "904447526427", // FCM sender_id 904447526427
        ])->content([
            'title' => 'Invoice Approved',
            'body' => 'test message from laravel package',
        ])->data([
            'title' => "Salesman {$notifiable->name} requesting for  discount",
            'image' => 'placeholder.jpg ',
            'message' => 'You got a new Message',
        ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.

        return $message;

    }

android mainfest

<service
    android:name=".utils.services.MessagingController"
    android:permission="signature"
    android:stopWithTask="true"
    android:enabled="true"
    android:exported="true"
    tools:ignore="Instantiatable">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

android function that received the notification

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    DLog("")
    val data = remoteMessage.data
    remoteMessage.notification?.let {
        data.set(NOTI_TITLE , it.title)
        data.set(NOTI_MESSAGE , it.body)
        //Add more data if necessary
    }
    pushNotification.show(applicationContext, data)
}

the notification is comming when my in forground is not coming when its background? any help please?

Omda
  • 157
  • 6
  • 19

1 Answers1

0

This took me some time browsing through code to figure out.

As stated in another SO answer you can send two types of messages:

  1. Display Messages: These messages trigger the onMessageReceived() callback only when your app is in foreground
  2. Data Messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed

It would seem that your problem is related to this information. An additional quote from the same answer: "NOTE: Be sure you're not adding JSON key notification".


With that information we can take a closer look at the two methods you're calling on FcmMessage that might be relevant: content() and data().

See line 101 for:

public function content(array $params)
{
    $this->notification = $params;

    return $this;
}

Then later in formatData() (line 282) which is called in FcmChannel we encounter this:

if (isset($this->notification) && count($this->notification)) {
    $payload['notification'] = $this->notification;
}

From here I'd say it's safe to assume that the payload is JSON encoded before being sent. So you are adding the JSON key 'notification', which you shouldn't as per the SO answer I linked to before.

Try something like this instead:

public function toFcm($notifiable)
    {
        $message = new FcmMessage();
        $message->setHeaders([
            'project_id' => "904447526427", // FCM sender_id 904447526427
        ])->data([
            'title' => 'Invoice Approved',
            'body' => 'test message from laravel package',
        ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.

        return $message;
    }
Niellles
  • 868
  • 10
  • 27