1

I implemented the firebase cloud messaging (FCM) service in my app and I get notifications with my custom layout but just when the app is in the foreground and the reason is clear, It is because of existing setCustomContentView(myContentView) in onMessageReceived and we all know that onMessageReceived handle notification just when the app is in the foreground.

Now, I'm going to know is there a way to set the same custom layout for the notifications which will be shown when the app is in the background?

3
  • 1
    Use data payload instead of notification payload which works for both foreground and background. Commented Jun 11, 2019 at 9:40
  • use data object instead of notification object Commented Jun 11, 2019 at 9:41
  • And how to set a custom layout? can you give me some code? Commented Jun 11, 2019 at 9:42

2 Answers 2

2

FCM has two messages types: when your app is in the background.

  • Notification message: enter image description here

  • Data message: enter image description here

    1. Notification message: the notification is delivered to the device’s system tray, not onMessageReceived

    2. Data message: always, the notification is delivered to onMessageReceived As image

so, You should send a format is Data Message to handle notification in onMessageReceived when the app is background or foreground

Sign up to request clarification or add additional context in comments.

Comments

1

I would advise to modify server code to receive data onMessageReceived in both foreground and background and ask sender to send only data payload without notification.

Here I have removed notification payload.

{"to":"[add your token]","data":{"title":"Working Good","body":"[add your message]"},"priority":"high"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.