Skip to content

Commit f0e00fe

Browse files
authored
Merge pull request #115 from CatalystCode/thcao/bug-fixes
Fixing exception occurred when FCM refreshes token
2 parents a679544 + 2bafc6a commit f0e00fe

File tree

7 files changed

+25
-10
lines changed

7 files changed

+25
-10
lines changed

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void onNewToken(String token) {
6969
Log.i(TAG, "Refreshing FCM Registration Token");
7070

7171
Intent intent = NotificationHubUtil.IntentFactory.createIntent(this, ReactNativeRegistrationIntentService.class);
72-
startService(intent);
72+
ReactNativeRegistrationIntentService.enqueueWork(this, intent);
7373
}
7474

7575
@Override

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void register(ReadableMap config, Promise promise) {
155155

156156
Intent intent = NotificationHubUtil.IntentFactory.createIntent(
157157
reactContext, ReactNativeRegistrationIntentService.class);
158-
reactContext.startService(intent);
158+
ReactNativeRegistrationIntentService.enqueueWork(reactContext, intent);
159159
}
160160

161161
@ReactMethod

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeRegistrationIntentService.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.azure.reactnative.notificationhub;
22

3-
import android.app.IntentService;
3+
import android.content.Context;
44
import android.content.Intent;
55

6+
import androidx.core.app.JobIntentService;
67
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
78

89
import android.util.Log;
@@ -15,18 +16,23 @@
1516
import java.util.concurrent.ExecutorService;
1617
import java.util.concurrent.Executors;
1718

18-
public class ReactNativeRegistrationIntentService extends IntentService {
19+
public class ReactNativeRegistrationIntentService extends JobIntentService {
1920

2021
public static final String TAG = "ReactNativeRegistration";
2122

23+
private static final int JOB_ID = 1000;
24+
2225
private final ExecutorService mPool = Executors.newFixedThreadPool(1);
2326

24-
public ReactNativeRegistrationIntentService() {
25-
super(TAG);
27+
/**
28+
* Convenience method for enqueuing work in to this service.
29+
*/
30+
public static void enqueueWork(Context context, Intent work) {
31+
enqueueWork(context, ReactNativeRegistrationIntentService.class, JOB_ID, work);
2632
}
2733

2834
@Override
29-
protected void onHandleIntent(Intent intent) {
35+
protected void onHandleWork(Intent intent) {
3036
final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
3137
final Intent event = NotificationHubUtil.IntentFactory.createIntent(TAG);
3238
final NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();

docs/android-installation.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ In `android/app/src/main/AndroidManifest.xml`
110110

111111
<service
112112
android:name="com.azure.reactnative.notificationhub.ReactNativeRegistrationIntentService"
113-
android:exported="false" />
113+
android:exported="false"
114+
android:permission="android.permission.BIND_JOB_SERVICE" />
114115

115116
<service
116117
android:name="com.azure.reactnative.notificationhub.ReactNativeFirebaseMessagingService"

sample/android/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
android:required="false" />
2020
<service
2121
android:name="com.azure.reactnative.notificationhub.ReactNativeRegistrationIntentService"
22-
android:exported="false" />
22+
android:exported="false"
23+
android:permission="android.permission.BIND_JOB_SERVICE" />
2324
<service
2425
android:name="com.azure.reactnative.notificationhub.ReactNativeFirebaseMessagingService"
2526
android:stopWithTask="false">

sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeFirebaseMessagingServiceTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
@PrepareForTest({
4848
NotificationHubUtil.class,
4949
ReactNativeNotificationsHandler.class,
50+
ReactNativeRegistrationIntentService.class,
5051
NotificationChannelBuilder.Factory.class,
5152
NotificationHubUtil.IntentFactory.class,
5253
Build.VERSION.class,
@@ -76,6 +77,7 @@ public void setUp() {
7677
PowerMockito.mockStatic(NotificationHubUtil.class);
7778
when(NotificationHubUtil.getInstance()).thenReturn(mHubUtil);
7879
PowerMockito.mockStatic(ReactNativeNotificationsHandler.class);
80+
PowerMockito.mockStatic(ReactNativeRegistrationIntentService.class);
7981
PowerMockito.mockStatic(NotificationChannelBuilder.Factory.class);
8082
PowerMockito.mockStatic(NotificationHubUtil.IntentFactory.class);
8183
PowerMockito.suppress(methodsDeclaredIn(FirebaseMessagingService.class));

sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeNotificationHubModuleTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.azure.reactnative.notificationhub.NotificationHubUtil;
3737
import com.azure.reactnative.notificationhub.ReactNativeNotificationHubModule;
3838
import com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler;
39+
import com.azure.reactnative.notificationhub.ReactNativeRegistrationIntentService;
3940
import com.facebook.react.bridge.Promise;
4041
import com.facebook.react.bridge.ReactApplicationContext;
4142
import com.facebook.react.bridge.ReactContext;
@@ -53,6 +54,7 @@
5354
LocalBroadcastManager.class,
5455
NotificationHubUtil.class,
5556
ReactNativeNotificationsHandler.class,
57+
ReactNativeRegistrationIntentService.class,
5658
GoogleApiAvailability.class
5759
})
5860
public class ReactNativeNotificationHubModuleTest {
@@ -97,6 +99,7 @@ public void setUp() {
9799
PowerMockito.mockStatic(NotificationHubUtil.class);
98100
when(NotificationHubUtil.getInstance()).thenReturn(mNotificationHubUtil);
99101
PowerMockito.mockStatic(ReactNativeNotificationsHandler.class);
102+
PowerMockito.mockStatic(ReactNativeRegistrationIntentService.class);
100103
PowerMockito.mockStatic(GoogleApiAvailability.class);
101104
when(GoogleApiAvailability.getInstance()).thenReturn(mGoogleApiAvailability);
102105

@@ -262,7 +265,9 @@ public void testRegisterSuccessfully() {
262265
verify(mNotificationHubUtil, times(1)).setTags(
263266
any(ReactContext.class), eq(tags));
264267
verify(mPromise, times(0)).reject(anyString(), anyString());
265-
verify(mReactApplicationContext, times(1)).startService(any(Intent.class));
268+
269+
PowerMockito.verifyStatic(ReactNativeRegistrationIntentService.class);
270+
ReactNativeRegistrationIntentService.enqueueWork(eq(mReactApplicationContext), any(Intent.class));
266271
}
267272

268273
@Test

0 commit comments

Comments
 (0)