coder12255
coder12255

Reputation: 67

Fcm Notification cannot show image if app close

I want it like this, when the app is in the background
I am sending notification and handle notification FirebaseMessagingService.
But if app on background FirebaseMessagingService cannot handle notification and not show image.
How can I do this? (If the application is in the foreground, the picture is shown in the notification.) And if I use onesignal image showing all times.

{
  "to" :"4sQrChOF16uMYYEKeiRz6dzCnN3m9OCE9jwOyPBOD92IlzljWvQ_1quiYyluP",
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
    },
  "data": {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark2222",
    "img_url" : "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRYJ_0NZFg0htNXwmKkyTv76bw05EacXaXNnqd4ZrPB7wVTXNxR"
  }
}

Upvotes: 3

Views: 3897

Answers (1)

Derryl Thomas
Derryl Thomas

Reputation: 1568

Here are 2 ways you can show an image in your notification consistently:

  1. [Recommended approach] - Using the newer HTTP V1 API of FCM: Here you can give an image URL as part of the notification payload and firebase will handle downloading and displaying the image for you. Here's the documentation link for the same.
  2. Using the older legacy API of FCM: Send a data-only payload(Don't send the notification object at all). Include image URL as part of the data payload. This will give you control in the onMessageReceived method(in both foreground and background cases). Here you can generate and display the notification however you like it. Retrieve the image URL here and download the image and create a notification with the image or however you want it to be rendered.

Upvotes: 2

Related Questions