3

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" />
  • Are you on Oreo or above? – Ishita Sinha Dec 31 '18 at 10:01
  • Do you have a `BroadcastReceiver` registered in your `AndroidManifest.xml`? – Reaz Murshed Dec 31 '18 at 10:03
  • @IshitaSinha yes Oreo !! – Mohammad Qo'ud Dec 31 '18 at 10:12
  • https://developer.android.com/about/versions/oreo/background – Ishita Sinha Dec 31 '18 at 10:13
  • 1
    @ReazMurshed Yes, everything works fine, the problem appears when the app is closed or the FirebaseMessagingServices killed, its just not receiving notifications anymore !! – Mohammad Qo'ud Dec 31 '18 at 10:15
  • Can you please see the answer I wrote below and check if you are missing something? – Reaz Murshed Dec 31 '18 at 10:16
  • @IshitaSinha I have read the article, and as I understood if the app handling a high-priority FCM message it supposes to keep running and never killed the service, I checked the battery and it's not optimized !! any advice !! – Mohammad Qo'ud Dec 31 '18 at 10:51
  • I had the same problem. I was doing some tidying up in the service's `onDestroy()` method, then I found that `onDestroy()` was being called whenever a message was received(!). When I removed my `onDestroy()` method, the app continued displaying messages when it was swiped away from the recent apps list. – Edmund Johnson Dec 02 '20 at 18:31

1 Answers1

1

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.

Chintak Patel
  • 748
  • 6
  • 24
  • That's exactly what I did, The server side always send data object and never use notification payload, The service registered perfectly in manifest but when the app goes to background, swiped from the open app list or killed the notification never arrived, I think that the FCM service itself killed by the android application manager or something else ... This problem occurs on android OREO !! do you know other solutions !! – Mohammad Qo'ud Dec 31 '18 at 11:35
  • Can you please post your `AndroidManifest` code where service is defined ? – Chintak Patel Dec 31 '18 at 11:42
  • and which version of `firebase-messaging` you are using in `build.gradle` ? – Chintak Patel Dec 31 '18 at 11:49
  • I updated the questing above and added the manifest code, yes I'm using the last version of firebase-messaging implementation "com.google.firebase:firebase-messaging:17.3.4" – Mohammad Qo'ud Dec 31 '18 at 11:59
  • I Viewed your code, it doesn't need for GCM broadcast permission and GCM related service. It is enough to have FCM Messaging Service and also FCM InstanceID Service is deprecated so don't use it. And can you please state that why you put `android:permission="android.permission.BIND_JOB_SERVICE"` in FCM Messaging Service? – Chintak Patel Dec 31 '18 at 12:04
  • I added the GCM broadcast because the FCM service not working well, and the permission it's a mistake it should be in the JobSerivce ... – Mohammad Qo'ud Dec 31 '18 at 12:09
  • The FCM service just works in the debug mode, it's not getting invoked when the app goes to background or swiped or killed... it is a Firebase issue or it's because of the Oreo System and its restrictions !?? – Mohammad Qo'ud Dec 31 '18 at 12:16