24
24
import android .os .Bundle ;
25
25
import android .os .Handler ;
26
26
import android .os .Looper ;
27
+ import android .text .TextUtils ;
27
28
import android .util .Log ;
28
- import java .util .Collections ;
29
- import java .util .Set ;
30
- import java .util .WeakHashMap ;
29
+ import java .util .ArrayDeque ;
30
+ import java .util .Queue ;
31
31
32
32
class FcmLifecycleCallbacks implements Application .ActivityLifecycleCallbacks {
33
33
34
- /** Keep a list of intents that we've seen to avoid accidentally logging events twice. */
35
- private final Set <Intent > seenIntents =
36
- Collections .newSetFromMap (new WeakHashMap <Intent , Boolean >());
34
+ private static final int RECENTLY_LOGGED_MESSAGE_IDS_MAX_SIZE = 10 ;
35
+
36
+ /** Last N message IDs that have been logged to prevent duplicate logging. */
37
+ private final Queue <String > recentlyLoggedMessageIds =
38
+ new ArrayDeque <>(RECENTLY_LOGGED_MESSAGE_IDS_MAX_SIZE );
37
39
38
40
// TODO(b/258424124): Migrate to go/firebase-android-executors
39
41
@ SuppressLint ("ThreadPoolCreation" )
40
42
@ Override
41
43
public void onActivityCreated (Activity createdActivity , Bundle instanceState ) {
42
44
Intent startingIntent = createdActivity .getIntent ();
43
- if (startingIntent == null || !seenIntents .add (startingIntent )) {
44
- // already seen (and logged) this, no need to go any further.
45
+ if (startingIntent == null ) {
45
46
return ;
46
47
}
47
48
@@ -57,12 +58,7 @@ public void onActivityCreated(Activity createdActivity, Bundle instanceState) {
57
58
}
58
59
59
60
@ Override
60
- public void onActivityPaused (Activity pausedActivity ) {
61
- if (pausedActivity .isFinishing ()) {
62
- // iff the activity is finished we can remove the intent from our "seen" list
63
- seenIntents .remove (pausedActivity .getIntent ());
64
- }
65
- }
61
+ public void onActivityPaused (Activity pausedActivity ) {}
66
62
67
63
@ Override
68
64
public void onActivityDestroyed (Activity destroyedActivity ) {}
@@ -84,6 +80,14 @@ private void logNotificationOpen(Intent startingIntent) {
84
80
try {
85
81
Bundle extras = startingIntent .getExtras ();
86
82
if (extras != null ) {
83
+ String messageId = MessagingAnalytics .getMessageId (extras );
84
+ if (!TextUtils .isEmpty (messageId )) {
85
+ if (recentlyLoggedMessageIds .contains (messageId )) {
86
+ // Already logged, don't log again.
87
+ return ;
88
+ }
89
+ recentlyLoggedMessageIds .add (messageId );
90
+ }
87
91
analyticsData = extras .getBundle (Constants .MessageNotificationKeys .ANALYTICS_DATA );
88
92
}
89
93
} catch (RuntimeException e ) {
0 commit comments