Android app not receiving Firebase Notification when app is stopped or killed by user or android OS

Viewed 4052

My android app is not receiving any firebase notification when the app is killed, the FirebaseMessagingService gets killed by the operating system when I close the application. Everything works fine when the app is in the foreground or in the background. When the app is in the background and I send a data message it's received by onMessageReceived() in my FirebaseMessagingService (which interestingly is created and then destroyed for each message i.e. FirebaseMessagingService). When I kill the app (by swiping from open app list) messages are no longer received in onMessageReceived(). All services get killed!! Can anyone please help me? Thanks in advance!

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- DefaultOrderedBroadcastReceiver will start this service to send notifications to system tray -->
        <service
            android:name=".fcm.SystemTrayNotificationsSenderIntentService"
            android:enabled="true"
            android:exported="false" />

        <!-- the FCM receiver service -->
        <service
            android:name=".fcm.FCMNotificationReceiverService"
            android:exported="true"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

<service
       android:name=".fcm.chat.FCMChatOfflineMessagesNotificationJobSchedulerService"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE" />
1 Answers

As per Firebase Documentation, it states that if from the server side, you are sending notification object only, it will call onMessageReceived() function when your app is in the foreground and will show in the system/notification tray if an app goes in background.

If you send both notification and data object in the same notification, it will call onMessageReceived() function in the foreground and when an app will be in the background, it will show notification in the tray and data object you will only get when you will tap on notification and moved to the intent you specified.

Now, when you will send data object only, app will call onMessageReceived() function every time.

So if you want to get executed onMessageReceived() every time, send data object only from your server side.

Related