Skip to content

Commit 9501fbd

Browse files
authored
Log notification open after onActivityCreated() on Android 7.0 and earlier. (#3432)
#3090 * Switched to log notification open after onActivityCreated() has completed on Android 7.0 and earlier since unparceling a Bundle is not thread safe on those versions. This may help to avoid a race condition with anything else that may be trying to access the Intent extra Bundle from another thread started in onActivityCreated()/onCreate().
1 parent 09fe06e commit 9501fbd

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

firebase-messaging/src/main/java/com/google/firebase/messaging/FcmLifecycleCallbacks.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
import android.app.Activity;
1717
import android.app.Application;
1818
import android.content.Intent;
19+
import android.os.Build.VERSION;
20+
import android.os.Build.VERSION_CODES;
1921
import android.os.Bundle;
22+
import android.os.Handler;
23+
import android.os.Looper;
2024
import java.util.Collections;
2125
import java.util.Set;
2226
import java.util.WeakHashMap;
@@ -35,12 +39,14 @@ public void onActivityCreated(Activity createdActivity, Bundle instanceState) {
3539
return;
3640
}
3741

38-
Bundle extras = startingIntent.getExtras();
39-
if (extras != null) {
40-
Bundle analyticsData = extras.getBundle(Constants.MessageNotificationKeys.ANALYTICS_DATA);
41-
if (MessagingAnalytics.shouldUploadScionMetrics(analyticsData)) {
42-
MessagingAnalytics.logNotificationOpen(analyticsData);
43-
}
42+
if (VERSION.SDK_INT <= VERSION_CODES.N) {
43+
// On Android 7.0 and lower Bundle unparceling is not thread safe. Wait to log notification
44+
// open after Activity.onCreate() has completed to try to avoid race conditions with other
45+
// code that may be trying to access the Intent extras Bundle in onCreate() on a different
46+
// thread.
47+
new Handler(Looper.getMainLooper()).post(() -> logNotificationOpen(startingIntent));
48+
} else {
49+
logNotificationOpen(startingIntent);
4450
}
4551
}
4652

@@ -66,4 +72,14 @@ public void onActivityResumed(Activity activity) {}
6672

6773
@Override
6874
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}
75+
76+
private void logNotificationOpen(Intent startingIntent) {
77+
Bundle extras = startingIntent.getExtras();
78+
if (extras != null) {
79+
Bundle analyticsData = extras.getBundle(Constants.MessageNotificationKeys.ANALYTICS_DATA);
80+
if (MessagingAnalytics.shouldUploadScionMetrics(analyticsData)) {
81+
MessagingAnalytics.logNotificationOpen(analyticsData);
82+
}
83+
}
84+
}
6985
}

0 commit comments

Comments
 (0)