FirebaseMessagingService onMessageReceived not call when app is killed or app not launch

1.2k views Asked by At

In normal case app is alive i received the notification and i display it.

{
    "to" : "xxxx",
     "notification": {
    "body": "Hello",
    "title" : "Hello world notification"
  },
    "data" : {
        "data1" : "dddddd",
    "data2" : "mmm"
    }
}

 <service
            android:name=".notifications.MyFirebaseMessagingService"
            android:directBootAware="true"
            android:exported="false"
            android:enabled="true"
            android:stopWithTask="false"
            >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

Since severals days i search a solution to receive notification FCM when my app is not alive.

I try to FirebaseMessagingService / stopWithTask Create other service to manage FirebaseMessagingService etc.

But i don't found a good way and good practice and technical solution.

Finally it not possible to receive notification when app if not alive?

what is the good way to keep app in background and not really killed app when the user swipe?

(i use API 29)

2

There are 2 answers

1
milan pithadia On

add this code in your Manifest file OnMessageRecive not call if App is background refer this document

https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
1
firozsaifi24 On

Well FYI, this method doesn't gets called when the app is in background or killed because The notifications are handled by Device OS when the app is in background or killed.

So if you do not want the notifications to be handled by Device OS and wants this method to be executed even in background and killed state then follow this:

  1. First of all, make FCM service like this by removing all unwanted codes

         <service android:name=".push.MyFirebaseMessagingService">
    
             <intent-filter>
                 <action android:name="com.google.firebase.MESSAGING_EVENT" />
             </intent-filter>
    
         </service>
    
  2. Importantly, Make changes in your fcm json payload like this by removing notification object to make OnReceivedMethod work.

    { "to" : "xxxx", "data" : { "bodyText": "Hello", "titleText" : "Hello world notification", "data1" : "dddddd", "data2" : "mmm", } }