5

I'm trying to send FCM messages to my device. I receive them correctly when app is in background or foreground. However, when my app is killed i.e. when removed from recent apps, I don't receive notifications if it is in the default "Optimized" battery usage mode. When I set my app's battery mode to "Not optimized", I receive notifications correctly even when app is killed.

And yes, I've tried sending FCM messages and FCM notifications with "High" Priority but logcat on recieving meassge shows this:

Error broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.flagg327.guicomaipu (has extras) }

All other apps (WhatsApp, Facebook etc.) on my device are in "Optimised" mode yet they receive push notifications even when they're killed.

I have tried installing signed apk version of my app still no response in background

Right now I have implemented a basic functionality

@Override
protected void onStart() {
    super.onStart();  
packageName = getApplication().getPackageName();
intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
pm = (PowerManager) 
getApplication().getSystemService(Context.POWER_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && 
!pm.isIgnoringBatteryOptimizations(packageName)) {

intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    getApplication().startActivity(intent);
        }
        }

This basically takes the user to settings and forces him to disable optimisation but this is a bad approach how can we progamatically solve this.

I'm using OnePlus 5T (Android 8.1.0). Can anyone explain why is this happening in my app specifically?and how to add my app to whitelist

can this be implemented heres my code for data payload

JSONObject root = new JSONObject();

                    JSONObject data = new JSONObject();

                    data.put("type", type);
                    data.put("message", message);
                    data.put("id", pNumber);
                    data.put("count",count);
                    JSONObject android = new JSONObject();
                    android.put("priority","high");
                    root.put("android",android);
                    root.put("data", data);
                    root.put("to", "/topics/"+recipients);



                    String result = postToFCM(root.toString());

Any help will be apreciated...

1 Answers1

0

Here is a solution to your problem.

  1. Go to your phone's Settings.

  2. Tap on Apps and then Application List.

  3. Scroll down to locate your app and tap on it.

  4. Tap on Notifications and ensure that it is enabled.

  5. In the same screen, tap on Battery.

  6. Tap on Battery Optimization.

  7. Scroll down to locate your app and tap on it.

  8. Select Don't Optimize and tap on Done.

Anubhav Gupta
  • 2,492
  • 14
  • 27