45

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.)

Eirik W
  • 3,086
  • 26
  • 29
  • 3
    Also seeing this, it's 99.9% Android 8.0 for me. – Learn OpenGL ES Sep 20 '17 at 12:16
  • 1
    [This adb command](https://developer.android.com/topic/performance/background-optimization.html#further-optimization) may be helpful to reproduce the problem: `adb shell cmd appops set RUN_IN_BACKGROUND ignore`. _simulates conditions where implicit broadcasts and background services are unavailable_ – Bob Snyder Sep 20 '17 at 14:35
  • 1
    The same to me, probably 100% of Android 8.0 has this ANR – ARLabs Sep 20 '17 at 17:12
  • Same here, and I'm using only analytics – Dimezis Nov 21 '17 at 10:58

2 Answers2

9

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.

Jeff
  • 2,425
  • 1
  • 18
  • 43
  • 12
    Well, it doesn't for me – fillobotto Oct 27 '18 at 10:30
  • @fillobotto please [contact support](https://firebase.google.com/support/) with your specific error message. They can help you out. – Jeff Oct 31 '18 at 11:00
  • They just said not to have FCM and Firebase to work together. This is not the case if they were referring to the Android project. However I was sending notification with old FCM from PHP backend. Looking forward to see if the new HTTP v1 API fixes this. – fillobotto Oct 31 '18 at 16:52
  • I got this error today 8 Jul 2022 – Sujith S Manjavana Jul 08 '22 at 07:18
2

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.

Ranjan
  • 1,096
  • 10
  • 22
  • Thank you for sharing what works for you. We will probably try to update to `targetSdkVersion 26` and let you know if this also works for us, but it will probably take som time for us because we would need to e.g. introduce notification channels and do other [migration steps](https://developer.android.com/about/versions/oreo/android-8.0-migration.html). PS: We are not using Firebase authentication, only Firebase messaging and Firebase analytics. – Eirik W Oct 16 '17 at 06:29
  • I am also getting ANR on android O devices, can any one help? – Cheerag Apr 27 '18 at 10:52