Android FCM not receiving notifications when app is removed from background
Asked Answered
L

2

9

The phone is OnePlus3T. The oxygen OS build is 4.1.6. App receives notification when the app is in foreground on in background but in the memory. But does not receive notification when the app is not in memory i.e. swiped out of the memory. The notification is received on other devices having android OS verions 4.2, 5.1.1, 6.0.1, 7.1.1 Lineage OS even the app is not in the memory.

Kindly suggest something. Thanks in advance.

Largeminded answered 30/6, 2017 at 13:35 Comment(4)
if its working on other devices, then it sounds like its something that manufacturer changed in the OS so I doubt there is anything you will be able to do about it.Sonnie
But then notifications for Whatsapp and Gmail are working.....I have a service for creating notifications apart from FirebaseMessagingService. It is START_NOT_STICKY. Tried START_STICKY, but still did not work. Am I doing something wrong?Largeminded
See my answer hereCorps
Possible duplicate of Android app not receiving Firebase Notification when app is stopped from multi-task trayPtero
E
24

I also faced the same issue.

Two ways to solve this issue

1- Using notification payload

notification payload can be send using data tag or notification tag.

using data tag

 "data" {
  "title": "welcome",
  "description" :"to your app" ,
  "image" :"image_url",  
  "deeplink" :"deeplink",
   - -
  }

it will trigger the FirebaseMessagingService onMessageReceived method. it will not work on some devices, when an app is in the background.

using notification

 "notification" {
   "title":"title",
   "description" : "description",
   "click_action" :"activity to be open"
   ..
}

this is handled android system try and show the notification, this case onMessageReceived method of your FirebaseMessagingService will not call.notification will be shown even app is in the background. one disadvantage of this is - you can't the customize notification because it's handled by the Android system. more info

2- Enable Auto start in device setting

when you send notification using data and app is killed notification will not be shown. if you observe the log cat you will see

W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.cabipool (has extras) }

can be solved in oneplus 3 setting -->Apps --> click on gear wheel -->last option App select app auto launch --> find your app enable the switch

this the problem in most of the devices like Vivo,Oppo,xiaomi,Asus, one plus 3.

steps to enable app auto launch very based on device manufacturer.

Update:

OnePlus 3 and Android 8.0 the Auto-start option was removed, so now you can go to Settings> Battery> Battery Optimisation> (Three dots menu on the left-top corner) Advanced optimization> Turn off Advanced Optimization.

Eh answered 22/8, 2017 at 6:53 Comment(3)
On OnePlus 3 and Android 8.0 the Auto-start option was removed, so now you can go to Settings> Battery> Battery Optimisation> (Three dots menu on left-top corner) Advanced optimisation > Turn off Advanced optimisation.Jacobean
notification or data which payload is compulsory calling FirebaseMessagingService class onMessageReceived() method? can you please tell me,i too have same issue in some devices? @Rahul DevanavarPanelist
@RevathiManoharan if you send the notification from firebase dashboard by default it will be notification payload. you have to use curl or to send the custom data payload.notification payload has limitations it only supports title and description.Eh
A
2

This looks like a problem of the specific device.

To check run this command while your app is closed (after a reboot or after swiping)

adb shell dumpsys package MY-PACKAGE | grep stopped

if you can read stopped=true it means that the manufacturer of your device implemented a non standard behavior consisting in "force-stopping" applications when they are swiped.

force-stopping is very similar to disabling the app until the user opens it again.
When the app is in that state many other Android API will not work! (broadcasts, alarms..)

If this is the problem, please please contact the manufacturer and request that they fix the device!

Alkane answered 3/7, 2017 at 19:7 Comment(3)
Thank you for the reply, I tried the command, and stopped = false. Yet the notifications do not appearLargeminded
It could be that the device has a different type of customization. If the app works on different devices it means that the code is correct.Alkane
i think its depend on package name when i test my package name stopped=true and when i add whatsapp package name stopped = false, if you have any idea please let me know i'm facing same issue.Parquet

© 2022 - 2024 — McMap. All rights reserved.