Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FCM not receiving notifications when app is removed from background

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.

like image 852
Rajas47Ashtikar Avatar asked Jun 30 '17 13:06

Rajas47Ashtikar


People also ask

How do I get notifications when an app is closed Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you handle notifications when an app is in foreground in Firebase?

Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.

How do I force push notifications on Android?

Go to Settings > Notifications & status bar > Notification Manager > select the app and turn on "Allow Notifications", "Display on Lockscreen", "Display at the top of the screen", "Notification Sound and Vibration", and "Priority notification". Then, restart your phone.

What is difference between GCM and FCM in Android?

FCM is a cloud platform that provides messages and push notifications for operating systems- ios and Android, and websites as well. Google Cloud Messaging is a messaging service that enables the message transfer from server to clients apps.


2 Answers

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.

like image 83
Rahul Devanavar Avatar answered Sep 28 '22 04:09

Rahul Devanavar


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!

like image 27
Diego Giorgini Avatar answered Sep 28 '22 03:09

Diego Giorgini