diff --git a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java index 283ed5c8..57d05f54 100644 --- a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java +++ b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java @@ -69,7 +69,7 @@ public void onNewToken(String token) { Log.i(TAG, "Refreshing FCM Registration Token"); Intent intent = NotificationHubUtil.IntentFactory.createIntent(this, ReactNativeRegistrationIntentService.class); - startService(intent); + ReactNativeRegistrationIntentService.enqueueWork(this, intent); } @Override diff --git a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java index 621e5ab0..d0ba9cfa 100644 --- a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java +++ b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java @@ -155,7 +155,7 @@ public void register(ReadableMap config, Promise promise) { Intent intent = NotificationHubUtil.IntentFactory.createIntent( reactContext, ReactNativeRegistrationIntentService.class); - reactContext.startService(intent); + ReactNativeRegistrationIntentService.enqueueWork(reactContext, intent); } @ReactMethod diff --git a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeRegistrationIntentService.java b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeRegistrationIntentService.java index d6ebdc54..b83e56b8 100644 --- a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeRegistrationIntentService.java +++ b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeRegistrationIntentService.java @@ -1,8 +1,9 @@ package com.azure.reactnative.notificationhub; -import android.app.IntentService; +import android.content.Context; import android.content.Intent; +import androidx.core.app.JobIntentService; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import android.util.Log; @@ -15,18 +16,23 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -public class ReactNativeRegistrationIntentService extends IntentService { +public class ReactNativeRegistrationIntentService extends JobIntentService { public static final String TAG = "ReactNativeRegistration"; + private static final int JOB_ID = 1000; + private final ExecutorService mPool = Executors.newFixedThreadPool(1); - public ReactNativeRegistrationIntentService() { - super(TAG); + /** + * Convenience method for enqueuing work in to this service. + */ + public static void enqueueWork(Context context, Intent work) { + enqueueWork(context, ReactNativeRegistrationIntentService.class, JOB_ID, work); } @Override - protected void onHandleIntent(Intent intent) { + protected void onHandleWork(Intent intent) { final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); final Intent event = NotificationHubUtil.IntentFactory.createIntent(TAG); final NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance(); diff --git a/docs/android-installation.md b/docs/android-installation.md index 9f4cb5d6..99270d9f 100644 --- a/docs/android-installation.md +++ b/docs/android-installation.md @@ -110,7 +110,8 @@ In `android/app/src/main/AndroidManifest.xml` + android:exported="false" + android:permission="android.permission.BIND_JOB_SERVICE" /> + android:exported="false" + android:permission="android.permission.BIND_JOB_SERVICE" /> diff --git a/sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeFirebaseMessagingServiceTest.java b/sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeFirebaseMessagingServiceTest.java index 37ac892b..e8f1ff7a 100644 --- a/sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeFirebaseMessagingServiceTest.java +++ b/sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeFirebaseMessagingServiceTest.java @@ -47,6 +47,7 @@ @PrepareForTest({ NotificationHubUtil.class, ReactNativeNotificationsHandler.class, + ReactNativeRegistrationIntentService.class, NotificationChannelBuilder.Factory.class, NotificationHubUtil.IntentFactory.class, Build.VERSION.class, @@ -76,6 +77,7 @@ public void setUp() { PowerMockito.mockStatic(NotificationHubUtil.class); when(NotificationHubUtil.getInstance()).thenReturn(mHubUtil); PowerMockito.mockStatic(ReactNativeNotificationsHandler.class); + PowerMockito.mockStatic(ReactNativeRegistrationIntentService.class); PowerMockito.mockStatic(NotificationChannelBuilder.Factory.class); PowerMockito.mockStatic(NotificationHubUtil.IntentFactory.class); PowerMockito.suppress(methodsDeclaredIn(FirebaseMessagingService.class)); diff --git a/sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeNotificationHubModuleTest.java b/sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeNotificationHubModuleTest.java index 142d2502..a936fb58 100644 --- a/sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeNotificationHubModuleTest.java +++ b/sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeNotificationHubModuleTest.java @@ -36,6 +36,7 @@ import com.azure.reactnative.notificationhub.NotificationHubUtil; import com.azure.reactnative.notificationhub.ReactNativeNotificationHubModule; import com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler; +import com.azure.reactnative.notificationhub.ReactNativeRegistrationIntentService; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; @@ -53,6 +54,7 @@ LocalBroadcastManager.class, NotificationHubUtil.class, ReactNativeNotificationsHandler.class, + ReactNativeRegistrationIntentService.class, GoogleApiAvailability.class }) public class ReactNativeNotificationHubModuleTest { @@ -97,6 +99,7 @@ public void setUp() { PowerMockito.mockStatic(NotificationHubUtil.class); when(NotificationHubUtil.getInstance()).thenReturn(mNotificationHubUtil); PowerMockito.mockStatic(ReactNativeNotificationsHandler.class); + PowerMockito.mockStatic(ReactNativeRegistrationIntentService.class); PowerMockito.mockStatic(GoogleApiAvailability.class); when(GoogleApiAvailability.getInstance()).thenReturn(mGoogleApiAvailability); @@ -262,7 +265,9 @@ public void testRegisterSuccessfully() { verify(mNotificationHubUtil, times(1)).setTags( any(ReactContext.class), eq(tags)); verify(mPromise, times(0)).reject(anyString(), anyString()); - verify(mReactApplicationContext, times(1)).startService(any(Intent.class)); + + PowerMockito.verifyStatic(ReactNativeRegistrationIntentService.class); + ReactNativeRegistrationIntentService.enqueueWork(eq(mReactApplicationContext), any(Intent.class)); } @Test