Skip to content

Commit 103e68c

Browse files
committed
Refactored code to test notification handler class.
1 parent 2f8064a commit 103e68c

File tree

6 files changed

+285
-27
lines changed

6 files changed

+285
-27
lines changed

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

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

33
import android.content.Context;
4+
import android.content.Intent;
45
import android.content.SharedPreferences;
6+
import android.os.Bundle;
57

68
import java.util.Arrays;
79
import java.util.HashSet;
810
import java.util.Set;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
913

1014
import com.facebook.react.bridge.ReactContext;
1115
import com.microsoft.windowsazure.messaging.NotificationHub;
1216

17+
import org.json.JSONException;
18+
import org.json.JSONObject;
19+
1320
public class NotificationHubUtil {
1421
private static NotificationHubUtil sharedNotificationHubUtilInstance = null;
1522

@@ -25,6 +32,8 @@ public class NotificationHubUtil {
2532
private static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights";
2633
private static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration";
2734

35+
private final ExecutorService mPool = Executors.newFixedThreadPool(1);
36+
2837
private boolean mIsForeground;
2938

3039
public static NotificationHubUtil getInstance() {
@@ -145,6 +154,31 @@ public NotificationHub createNotificationHub(String hubName, String connectionSt
145154
return hub;
146155
}
147156

157+
public JSONObject convertBundleToJSON(Bundle bundle) {
158+
JSONObject json = new JSONObject();
159+
Set<String> keys = bundle.keySet();
160+
for (String key : keys) {
161+
try {
162+
json.put(key, bundle.get(key));
163+
} catch (JSONException e) {
164+
}
165+
}
166+
167+
return json;
168+
}
169+
170+
public Intent createBroadcastIntent(String action, JSONObject json) {
171+
Intent intent = new Intent(action);
172+
intent.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
173+
intent.putExtra("data", json.toString());
174+
175+
return intent;
176+
}
177+
178+
public void runInWorkerThread(Runnable runnable) {
179+
mPool.execute(runnable);
180+
}
181+
148182
private String getPref(Context context, String key) {
149183
SharedPreferences prefs =
150184
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,19 @@ private ReactNativeNotificationsHandler() {
3737
}
3838

3939
public static void sendBroadcast(final Context context, final Bundle bundle, final long delay) {
40-
(new Thread() {
40+
final NotificationHubUtil hubUtil = NotificationHubUtil.getInstance();
41+
hubUtil.runInWorkerThread(new Runnable() {
4142
public void run() {
4243
try {
4344
Thread.currentThread().sleep(delay);
44-
JSONObject json = new JSONObject();
45-
Set<String> keys = bundle.keySet();
46-
for (String key : keys) {
47-
try {
48-
json.put(key, bundle.get(key));
49-
} catch (JSONException e) {
50-
}
51-
}
52-
53-
Intent event = new Intent(TAG);
54-
event.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
55-
event.putExtra("data", json.toString());
45+
JSONObject json = hubUtil.convertBundleToJSON(bundle);
46+
Intent event = hubUtil.createBroadcastIntent(TAG, json);
5647
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
5748
localBroadcastManager.sendBroadcast(event);
5849
} catch (Exception e) {
5950
}
6051
}
61-
}).start();
52+
});
6253
}
6354

6455
public static void sendNotification(Context context, Bundle bundle, String notificationChannelID) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ReactNativeRegistrationIntentService extends IntentService {
1919

2020
public static final String TAG = "ReactNativeRegistration";
2121

22-
private final ExecutorService pool = Executors.newFixedThreadPool(1);
22+
private final ExecutorService mPool = Executors.newFixedThreadPool(1);
2323

2424
public ReactNativeRegistrationIntentService() {
2525
super(TAG);
@@ -43,7 +43,7 @@ protected void onHandleIntent(Intent intent) {
4343
}
4444

4545
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(
46-
pool, new OnSuccessListener<InstanceIdResult>() {
46+
mPool, new OnSuccessListener<InstanceIdResult>() {
4747
@Override
4848
public void onSuccess(InstanceIdResult instanceIdResult) {
4949
try {

sample/android/app/build.gradle

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ android {
135135
versionCode 1
136136
versionName "1.0"
137137
}
138+
138139
splits {
139140
abi {
140141
reset()
@@ -143,6 +144,7 @@ android {
143144
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
144145
}
145146
}
147+
146148
signingConfigs {
147149
debug {
148150
storeFile file('debug.keystore')
@@ -151,6 +153,7 @@ android {
151153
keyPassword 'android'
152154
}
153155
}
156+
154157
buildTypes {
155158
debug {
156159
signingConfig signingConfigs.debug
@@ -163,6 +166,7 @@ android {
163166
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
164167
}
165168
}
169+
166170
// applicationVariants are e.g. debug, release
167171
applicationVariants.all { variant ->
168172
variant.outputs.each { output ->
@@ -186,8 +190,9 @@ dependencies {
186190
implementation "com.google.firebase:firebase-messaging:17.6.0"
187191
implementation "com.google.firebase:firebase-core:16.0.8"
188192
implementation "com.microsoft.azure:notification-hubs-android-sdk:0.6@aar"
189-
testImplementation "org.powermock:powermock-api-mockito:1.6.2"
190-
testImplementation "org.powermock:powermock-module-junit4:1.6.2"
193+
testImplementation "org.powermock:powermock-core:2.0.2"
194+
testImplementation "org.powermock:powermock-api-mockito2:2.0.2"
195+
testImplementation "org.powermock:powermock-module-junit4:2.0.2"
191196

192197
if (enableHermes) {
193198
def hermesPath = "../../node_modules/hermes-engine/android/";

0 commit comments

Comments
 (0)