Firebase onMessageReceived not called when app in background

Pushing notification to your application is not as difficult as we thought.With firebase it is few minute of work and you are good to go.

Firebase push notification with foreground and background kotlin
Photo by Jamie Street on Unsplash

Hello guys, My name is Tashi and In this shitty post we will see how to push notification in your application with onMessageReceived called in both foreground and background.

There are different way to send notification such as

a)Notification messages

b)Data messages

c)Messages with both notification and data payloads

We will use data messages as it is the only way to called onMessageReceived method in foreground,background or killed.

Lets get start

  1. I won’t waste your time creating new application and connecting with firebase rather we will go straight to push notification.
  2. First things is to add firebase dependency in your application

go to->tool->firebase->cloud messaging and click on add fcm

3. Now that it is connected Subcribe to topic so that we can send notification to any group or all the device base on topic. I will send notification to all device and to do that. all application should subcribe to yopic called “all’’

I have created application class and create topic called “all ” and subscribe to it. PrayerApp.kt

4.Now that we set the topic,We will send data such as title,body and description from postman later to this topic .

When we send this message to this topic, our application will listen to this topic and onMessageReceived will get called . For that Create new class for firebase messaging service as MessagingService.kt

Now that we have handle message and display notification from this sevice class. lets update in AndroidManifest.xml too.

5. Update AndroidManifest.kt

That all, now run the application and wait for the notification.

Now to trigger notification get API from your firebase console and open postman and update as below. Make sure you have data since this is data message.

Enter firebase pushnotification url:

Also update Header as below. to get Authorization key firebase console and add “key=” as prefix.

Now click on send button and you shoud get the output by now whether application is in foreground, background or killed.

If you noticed now onMessageReceived method will also called no matter what your application state is.

That all,Thanks guys, I hope you like this shitty blog. please make sure to give clap 👏 and leave some engagement. let me know in comment section if you face any issue.

Checkout more blog from CodingWithTashi. Follow me on twitter or Instagram or YouTube.




Native Android / Flutter Developer | Flutter enthusiast | Content Creator

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What we are watching 📺

Node.js Native Addon for Secure Reliable Transport

Simple Vue Libraries for Adding Input Controls

Publish Github Package

Add Feature Toggling to your Angular app (The Ultimate Guide 2020)

Differences between React and React Native that newcomers should knows about

LeBron James ‘Mode’ Launches on Pandora’s UNINTERRUPTED Radio

How to simply test macros and transformations in Kentico

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Native Android / Flutter Developer | Flutter enthusiast | Content Creator

More from Medium

Flutter Project Build Warning: Mapping new ns Warning Solution

Build a Beautiful Navigation Drawer/Sidebar Menu in Flutter

How to setup Flutter development environment for Android on Linux… I mean Windows

Passing data from one to another screen on Flutter