From 226de0920b448bc71c78161c2e4d2049b6dc21b9 Mon Sep 17 00:00:00 2001 From: Alex Ames Date: Wed, 26 May 2021 11:00:05 -0700 Subject: [PATCH 1/2] Converted RegistrationIntentService to a JobIntentService There appears to be a hard-to-reproduce hang that in the RegistrationIntentService, which is probably related to it being an IntentService instead of a JobIntentService. --- messaging/AndroidManifest.xml | 1 + .../com/google/firebase/messaging/JobIds.java | 1 + .../messaging/cpp/RegistrationIntentService.java | 16 ++++++++++++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/messaging/AndroidManifest.xml b/messaging/AndroidManifest.xml index 32d3869b2..45f1e91e1 100644 --- a/messaging/AndroidManifest.xml +++ b/messaging/AndroidManifest.xml @@ -22,6 +22,7 @@ diff --git a/messaging/src/android/java/com/google/firebase/messaging/JobIds.java b/messaging/src/android/java/com/google/firebase/messaging/JobIds.java index c24e06ce7..9cfca6fb1 100644 --- a/messaging/src/android/java/com/google/firebase/messaging/JobIds.java +++ b/messaging/src/android/java/com/google/firebase/messaging/JobIds.java @@ -20,4 +20,5 @@ public final class JobIds { private JobIds() {} public static final int MESSAGE_FORWARDING_SERVICE = 1000; + public static final int REGISTRATION_INTENT_SERVICE = 1001; } diff --git a/messaging/src/android/java/com/google/firebase/messaging/cpp/RegistrationIntentService.java b/messaging/src/android/java/com/google/firebase/messaging/cpp/RegistrationIntentService.java index 67ddc9c45..bd8da2f6b 100644 --- a/messaging/src/android/java/com/google/firebase/messaging/cpp/RegistrationIntentService.java +++ b/messaging/src/android/java/com/google/firebase/messaging/cpp/RegistrationIntentService.java @@ -14,7 +14,6 @@ package com.google.firebase.messaging.cpp; -import android.app.IntentService; import android.content.Context; import android.content.Intent; import com.google.firebase.iid.FirebaseInstanceId; @@ -28,7 +27,7 @@ * A class that manages Registration Token generation and passes the generated tokens to the native * OnTokenReceived function. */ -public class RegistrationIntentService extends IntentService { +public class RegistrationIntentService extends JobIntentService { private static final String TAG = "FirebaseRegService"; public RegistrationIntentService() { @@ -37,11 +36,20 @@ public RegistrationIntentService() { super(TAG); } + /** + * Convenience wrapper over enqueueWork to either directly start the service (when running on + * pre-O platforms) or enqueue work for it as a job (when running on Android O and later). + */ + public static void enqueueWork(Context context, Intent intent) { + enqueueWork( + context, RegistrationIntentService.class, JobIds.REGISTRATION_INTENT_SERVICE, intent); + } + // Fetch the latest registration token and notify the C++ layer. @Override - protected void onHandleIntent(Intent intent) { + protected void onHandleWork(@NonNull Intent intent) { String token = FirebaseInstanceId.getInstance().getToken(); - DebugLogging.log(TAG, String.format("onHandleIntent token=%s", token)); + DebugLogging.log(TAG, String.format("onHandleWork token=%s", token)); if (token != null) { writeTokenToInternalStorage(this, token); } From f0e61d3bd97d8f3c692ea0bf0b9260d390f8d5a4 Mon Sep 17 00:00:00 2001 From: Alex Ames Date: Fri, 4 Jun 2021 01:49:41 -0700 Subject: [PATCH 2/2] Minor fixes to RegistrationIntentService --- .../cpp/RegistrationIntentService.java | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/messaging/src/android/java/com/google/firebase/messaging/cpp/RegistrationIntentService.java b/messaging/src/android/java/com/google/firebase/messaging/cpp/RegistrationIntentService.java index bd8da2f6b..83010ca0e 100644 --- a/messaging/src/android/java/com/google/firebase/messaging/cpp/RegistrationIntentService.java +++ b/messaging/src/android/java/com/google/firebase/messaging/cpp/RegistrationIntentService.java @@ -16,6 +16,7 @@ import android.content.Context; import android.content.Intent; +import androidx.core.app.JobIntentService; import com.google.firebase.iid.FirebaseInstanceId; import com.google.flatbuffers.FlatBufferBuilder; import java.io.FileOutputStream; @@ -30,24 +31,9 @@ public class RegistrationIntentService extends JobIntentService { private static final String TAG = "FirebaseRegService"; - public RegistrationIntentService() { - // The tag here is used only to name the worker thread; it's important only for debugging. - // http://developer.android.com/reference/android/app/IntentService.html#IntentService(java.lang.String) - super(TAG); - } - - /** - * Convenience wrapper over enqueueWork to either directly start the service (when running on - * pre-O platforms) or enqueue work for it as a job (when running on Android O and later). - */ - public static void enqueueWork(Context context, Intent intent) { - enqueueWork( - context, RegistrationIntentService.class, JobIds.REGISTRATION_INTENT_SERVICE, intent); - } - // Fetch the latest registration token and notify the C++ layer. @Override - protected void onHandleWork(@NonNull Intent intent) { + protected void onHandleWork(Intent intent) { String token = FirebaseInstanceId.getInstance().getToken(); DebugLogging.log(TAG, String.format("onHandleWork token=%s", token)); if (token != null) {