From 94b07f9af085b6ef8b40d15210efe0dd156298ba Mon Sep 17 00:00:00 2001 From: My Name <myemail@chromium.org> Date: Fri, 7 Mar 2025 14:10:09 -0500 Subject: [PATCH 1/3] Store registered context for sync task unregistrations --- .../java/com/google/firebase/messaging/SyncTask.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java b/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java index c0c4074c11e..fb42af037e7 100644 --- a/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java +++ b/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java @@ -161,6 +161,7 @@ boolean isDeviceConnected() { static class ConnectivityChangeReceiver extends BroadcastReceiver { @Nullable private SyncTask task; // task is set to null after it has been fired. + @Nullable private Context context; public ConnectivityChangeReceiver(SyncTask task) { this.task = task; @@ -171,7 +172,8 @@ public void registerReceiver() { Log.d(TAG, "Connectivity change received registered"); } IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); - task.getContext().registerReceiver(this, intentFilter); + context = task.getContext(); + context.registerReceiver(this, intentFilter); } @Override @@ -191,8 +193,10 @@ public void onReceive(Context context, Intent intent) { Log.d(TAG, "Connectivity changed. Starting background sync."); } task.firebaseMessaging.enqueueTaskWithDelaySeconds(task, 0); - task.getContext().unregisterReceiver(this); - task = null; + if(context != null) { + context.unregisterReceiver(this); + task = null; + } } } From 0b970a4ea7311eafaf7be7dda5184f186e4e1d07 Mon Sep 17 00:00:00 2001 From: My Name <myemail@chromium.org> Date: Fri, 7 Mar 2025 14:50:06 -0500 Subject: [PATCH 2/3] fix formatting --- .../google/firebase/messaging/SyncTask.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java b/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java index fb42af037e7..532d3ddde18 100644 --- a/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java +++ b/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java @@ -35,7 +35,9 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -/** Background task to perform sync operations with the Firebase backend using a bg thread */ +/** + * Background task to perform sync operations with the Firebase backend using a bg thread + */ class SyncTask implements Runnable { private final long nextDelaySeconds; @@ -113,9 +115,9 @@ public void run() { * Refreshes the token if needed * * @return {@code true} if successful, {@code false} if needs to be rescheduled. - * @throws IOException on a hard failure that should not be retried. Hard failures are failures - * except {@link GmsRpc#ERROR_SERVICE_NOT_AVAILABLE} and {@link - * GmsRpc#ERROR_INTERNAL_SERVER_ERROR} + * @throws IOException on a hard failure that should not be retried. Hard failures are + * failures except {@link GmsRpc#ERROR_SERVICE_NOT_AVAILABLE} and + * {@link GmsRpc#ERROR_INTERNAL_SERVER_ERROR} */ @VisibleForTesting boolean maybeRefreshToken() throws IOException { @@ -160,8 +162,10 @@ boolean isDeviceConnected() { @VisibleForTesting static class ConnectivityChangeReceiver extends BroadcastReceiver { - @Nullable private SyncTask task; // task is set to null after it has been fired. - @Nullable private Context context; + @Nullable + private SyncTask task; // task is set to null after it has been fired. + @Nullable + private Context context; public ConnectivityChangeReceiver(SyncTask task) { this.task = task; @@ -193,7 +197,7 @@ public void onReceive(Context context, Intent intent) { Log.d(TAG, "Connectivity changed. Starting background sync."); } task.firebaseMessaging.enqueueTaskWithDelaySeconds(task, 0); - if(context != null) { + if (context != null) { context.unregisterReceiver(this); task = null; } From 8f8d9c3d020feb1300b6de2f4da192c66b17e53e Mon Sep 17 00:00:00 2001 From: My Name <myemail@chromium.org> Date: Fri, 7 Mar 2025 18:53:57 -0500 Subject: [PATCH 3/3] fix formatting --- .../google/firebase/messaging/SyncTask.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java b/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java index 532d3ddde18..9969d45305d 100644 --- a/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java +++ b/firebase-messaging/src/main/java/com/google/firebase/messaging/SyncTask.java @@ -35,9 +35,7 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -/** - * Background task to perform sync operations with the Firebase backend using a bg thread - */ +/** Background task to perform sync operations with the Firebase backend using a bg thread */ class SyncTask implements Runnable { private final long nextDelaySeconds; @@ -115,9 +113,9 @@ public void run() { * Refreshes the token if needed * * @return {@code true} if successful, {@code false} if needs to be rescheduled. - * @throws IOException on a hard failure that should not be retried. Hard failures are - * failures except {@link GmsRpc#ERROR_SERVICE_NOT_AVAILABLE} and - * {@link GmsRpc#ERROR_INTERNAL_SERVER_ERROR} + * @throws IOException on a hard failure that should not be retried. Hard failures are failures + * except {@link GmsRpc#ERROR_SERVICE_NOT_AVAILABLE} and {@link + * GmsRpc#ERROR_INTERNAL_SERVER_ERROR} */ @VisibleForTesting boolean maybeRefreshToken() throws IOException { @@ -162,10 +160,8 @@ boolean isDeviceConnected() { @VisibleForTesting static class ConnectivityChangeReceiver extends BroadcastReceiver { - @Nullable - private SyncTask task; // task is set to null after it has been fired. - @Nullable - private Context context; + @Nullable private SyncTask task; // task is set to null after it has been fired. + @Nullable private Context context; public ConnectivityChangeReceiver(SyncTask task) { this.task = task; @@ -176,8 +172,10 @@ public void registerReceiver() { Log.d(TAG, "Connectivity change received registered"); } IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); - context = task.getContext(); - context.registerReceiver(this, intentFilter); + if (task != null) { + context = task.getContext(); + context.registerReceiver(this, intentFilter); + } } @Override @@ -197,8 +195,8 @@ public void onReceive(Context context, Intent intent) { Log.d(TAG, "Connectivity changed. Starting background sync."); } task.firebaseMessaging.enqueueTaskWithDelaySeconds(task, 0); - if (context != null) { - context.unregisterReceiver(this); + if (this.context != null) { + this.context.unregisterReceiver(this); task = null; } }