We have an Android app that have many ANR errors reported lately. This only occurs on Android 7.1 and 8.0 (not on e.g. 4.4, 5.0 or 6.0). The ANR is:
Broadcast of Intent { act=com.google.firebase.INSTANCE_ID_EVENT flg=0x14 cmp=com.our.package.name/com.google.firebase.iid.FirebaseInstanceIdInternalReceiver (has extras) }
The question is: Why do we get this ANR, and what can we do to avoid this? Note that this works fine on earlier Android-versions, which in my opinion proves that we do not do any of the rookie-mistakes causing ANR.
I am having a really hard time reproducing this bug. Since it is only on Android 7.1 and 8.0 I think it may have to do with the new doze mode and battery saving, but even using adb shell dumpsys deviceidle force-idle
etc. while testing does not reproduce this issue, neither does putting in SystemClock.sleep(20000);
several places.
Our code for InstanceIdService
is:
public class InstanceIdService extends FirebaseInstanceIdService {
private Analytics mAnalytics;
@Override
public void onCreate() {
super.onCreate();
mAnalytics = new AnalyticsImpl();
boolean isFullVersion = getApplicationContext().getPackageName().endsWith("full");
mAnalytics.init(getApplicationContext(), isFullVersion);
}
@Override
public void onTokenRefresh() {
boolean initialLoginSucceeded = OurAppNameApplication.getInstance().getSettings().getInitialLoginSucceeded();
mAnalytics.logEvent("FCM_Token_Refresh_Triggered", "initialLoginSucceeded", String.valueOf(initialLoginSucceeded));
if (initialLoginSucceeded) { // We only report the FCM token to our server if the user has logged in at least once
OurAppNameApplication.getInstance().getOurAppNameService().registerDeviceWithRetry();
}
}
}
We use Google Play Services and Firebase version 11.2.0. Our targetSdkVersion is 25.
PS: The code mAnalytics.init(...)
above gives us a StrictMode warning, as this initializes Flurry. But this is disk access, not network traffic. And putting in SystemClock.sleep(20000);
at this location does not trigger any ANR.
Why do we get an ANR, and what can we do to avoid this?
--
Edit: As per the suggestion in the comment from Bob Snyder, I have tried to test with adb shell cmd appops set com.our.package.name RUN_IN_BACKGROUND ignore
. However, this does not produce any ANR, it only stops our Broadcast receiver from running, as shown in logcat:
09-21 10:39:25.314 943-6730/? W/ActivityManager: Background start not allowed: service Intent { act=com.google.firebase.INSTANCE_ID_EVENT pkg=com.our.package.name cmp=com.our.package.name/com.our.package.service.notifications.InstanceIdService (has extras) } to com.our.package.name/com.our.package.service.notifications.InstanceIdService from pid=4062 uid=10139 pkg=com.our.package.name
09-21 10:39:25.314 4062-4062/com.our.package.name E/FirebaseInstanceId: Error while delivering the message: ServiceIntent not found.
My conclusion is that this may not be a correct way to reproduce this ANR error.
For completeness sake: All ADB commands used when testing are:
adb shell dumpsys deviceidle force-idle
adb shell dumpsys battery unplug
adb shell am set-inactive com.our.package.name true
adb install -r our-app.apk
adb shell cmd appops set com.our.package.name RUN_IN_BACKGROUND ignore
(Actually - the last line is run many times in parallel with the adb install
so that we are sure it takes effect before the installation and restore (of settings) is done and Firebase registration token is automatically refreshed after the installation.)
This is a bug that was fixed in the September 18 release of the FCM SDK:
Fixed an issue that would occasionally cause apps to crash with Android Not Responding (ANR) errors when receiving a message.
Updating to com.google.firebase:firebase-messaging:17.3.2
or later should fix the issue. If it doesn't, please contact support.
Try the latest version of Firebase
and Play Services SDKs
(v. 11.4.2).
Also change your targetSDKVersion
to 26 and BuildToolsVersion
to 26.0.2.
I was also receiving the same error for Google Pixel/Nexus devices running on Android 8.0. I haven't received any new report after updating all the libraries.
Why do we get this ANR, and what can we do to avoid this?
Not really sure why this is happening. I contacted Firebase support as well to know the reasons but they asked for mcve and since I don't know what is causing the problem I wasn't able to provide mcve. I am using only Firebase authentication in my app, so I firmly believe the issue has something to do with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With