I\'m receiving that error from Android Studio\'s Android Monitor. This error appears when I send a push notification through GCM, in a real device, and the app has not been
I was also facing the issue. When I remove application from stack, push notification not received. After lots of Google, I found that few phones have battery optimization feature and this will disable background run of applications. When I enable that my application can also run in background. Push notification working fine. Only few application like What's application etc can only have default enable feature. You can have a look on this below URL.
https://github.com/firebase/quickstart-android/issues/89
To fix this issue there is no official solution but here is workaround you can use
2 things you need to manage Autostart and Battery Optimization then your issue will get fixed
you can use below intents for it
private void enableAutoStart() {
if (Build.BRAND.equalsIgnoreCase("xiaomi")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);
}
})
.show();
} else if (Build.BRAND.equalsIgnoreCase("Letv")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.letv.android.letvsafe",
"com.letv.android.letvsafe.AutobootManageActivity"));
startActivity(intent);
}
})
.show();
} else if (Build.BRAND.equalsIgnoreCase("Honor")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.huawei.systemmanager",
"com.huawei.systemmanager.optimize.process.ProtectActivity"));
startActivity(intent);
}
})
.show();
} else if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
try {
Intent intent = new Intent();
intent.setClassName("com.coloros.safecenter",
"com.coloros.safecenter.permission.startup.StartupAppListActivity");
startActivity(intent);
} catch (Exception e) {
try {
Intent intent = new Intent();
intent.setClassName("com.oppo.safe",
"com.oppo.safe.permission.startup.StartupAppListActivity");
startActivity(intent);
} catch (Exception ex) {
try {
Intent intent = new Intent();
intent.setClassName("com.coloros.safecenter",
"com.coloros.safecenter.startupapp.StartupAppListActivity");
startActivity(intent);
} catch (Exception exx) {
}
}
}
}
})
.show();
} else if (Build.MANUFACTURER.contains("vivo")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background.Our app runs in background to detect when you are in distress.")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.iqoo.secure",
"com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
startActivity(intent);
} catch (Exception e) {
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
startActivity(intent);
} catch (Exception ex) {
try {
Intent intent = new Intent();
intent.setClassName("com.iqoo.secure",
"com.iqoo.secure.ui.phoneoptimize.BgStartUpManager");
startActivity(intent);
} catch (Exception exx) {
ex.printStackTrace();
}
}
}
}
})
.show();
}
}
public boolean checkServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(
Integer.MAX_VALUE)) {
if ("com.sac.speechdemo.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Battery Optimization/Background Free/ DOZE Mode
to manage battery optimization use below code with permission, it will work on Stock Android
private void askIgnoreOptimization() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, IGNORE_BATTERY_OPTIMIZATION_REQUEST);
} else {
openNextActivity();
}
}
and for custom OS you need to redirect user for battery optimization permission like below intent for OPPO devices
intent.setClassName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerConsumptionActivity"); startActivity(intent);
call above intent, it will redirect you to battery option, "Disable Background Freeze, Abnormal Apps Optimization and Doze from \"Energy Saver ->youAPP"
Note: Once you call the above intent, there you may get different options to turn off Battery saving options. This battery optimization option can be found in Battery option from device setting, it varies as per ROM.
Since none of answers provide official link, I decided to ask firebase team and got official answer from them
It looks like you have an issue when your app is force stopped or killed. Actually, this is working as intended. The Android framework advises that apps that have been stopped (i.e. killed/force-stopped from Settings) should not be started without explicit user interaction. FCM follows this recommendation and thus its services will not start as well. This also means that messages will not be received (FirebaseMessagingService will not be called) when the app is in "killed" state. Here are some useful links so you could have a better understanding on this topic: https://developer.android.com/about/versions/android-3.1#launchcontrols https://github.com/firebase/quickstart-android/issues/368#issuecomment-343567506
In sum:
NOTE: even the question is about GCM, but FCM throw exact the same error in the question title.
I got the same Error
09-13 21:21:44.800 1851-1851/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.XXX.XXX (has extras) }
after killing the app.
After some research I realized, that this is only a "Debug-Issue". A "signed APK" handles the broadcast correctly even if the app has been shut down.
Hope it helps!
This is because your app is optimized for battery usage to disable that and receive notification even when your app isn't running in background
Add this permission to manifest
<uses-permission
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
While first launch of your app request user for permission to ignore this app for battery optimization
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
After digging into why this happens, it appears that it happens if you kill the app from Android Studio.
If you open the app from the launcher and swipe it away, your app will still receive notifications.
(Tested on OnePlus One with FCM not GCM)