Skip to content

Commit 3440cc1

Browse files
welishrtejasd
authored andcommitted
Store registered context for sync task unregistrations (#6752)
For issue #6558, this is an attempt at fixing the IllegalArgumentException by ensuring that the context we use for registering the SyncTask is the same context we use to unregister the task. Race conditions dont seem like a culprit here since unregister is only triggered by the Receiver itself, which should be only executed synchronously on the main thread.
1 parent 606a4bb commit 3440cc1

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

firebase-messaging/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Unreleased
2+
* [changed] Bug fix in SyncTask to always unregister the receiver on the same
3+
context on which it was registered.
24

35

46
# 24.1.0

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ boolean isDeviceConnected() {
161161
static class ConnectivityChangeReceiver extends BroadcastReceiver {
162162

163163
@Nullable private SyncTask task; // task is set to null after it has been fired.
164+
@Nullable private Context receiverContext;
164165

165166
public ConnectivityChangeReceiver(SyncTask task) {
166167
this.task = task;
@@ -171,7 +172,10 @@ public void registerReceiver() {
171172
Log.d(TAG, "Connectivity change received registered");
172173
}
173174
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
174-
task.getContext().registerReceiver(this, intentFilter);
175+
if (task != null) {
176+
receiverContext = task.getContext();
177+
receiverContext.registerReceiver(this, intentFilter);
178+
}
175179
}
176180

177181
@Override
@@ -191,7 +195,9 @@ public void onReceive(Context context, Intent intent) {
191195
Log.d(TAG, "Connectivity changed. Starting background sync.");
192196
}
193197
task.firebaseMessaging.enqueueTaskWithDelaySeconds(task, 0);
194-
task.getContext().unregisterReceiver(this);
198+
if (receiverContext != null) {
199+
receiverContext.unregisterReceiver(this);
200+
}
195201
task = null;
196202
}
197203
}

0 commit comments

Comments
 (0)