Skip to content

Commit a679544

Browse files
authored
Merge pull request #113 from CatalystCode/thcao/hubutil-unit-tests
Improve code coverage for Android
2 parents 9d6de64 + 5ff5f54 commit a679544

11 files changed

+1017
-33
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ public class NotificationChannelBuilder {
1212
private boolean mEnableLights = true;
1313
private boolean mEnableVibration = true;
1414

15-
public NotificationChannelBuilder() {
15+
public static class Factory {
16+
public static NotificationChannelBuilder create() {
17+
return new NotificationChannelBuilder();
18+
}
19+
}
20+
21+
private NotificationChannelBuilder() {
1622
}
1723

1824
public NotificationChannel build() {

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

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.json.JSONException;
2626
import org.json.JSONObject;
2727

28+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.ERROR_ACTIVITY_CLASS_NOT_FOUND;
2829
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.ERROR_COVERT_ACTIONS;
2930
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.ERROR_GET_ACTIONS_ARRAY;
3031
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_INTENT_NOTIFICATION;
@@ -50,22 +51,36 @@ public class NotificationHubUtil {
5051

5152
private static NotificationHubUtil sharedNotificationHubUtilInstance = null;
5253

53-
private static final String SHARED_PREFS_NAME = "com.azure.reactnative.notificationhub.NotificationHubUtil";
54-
private static final String KEY_FOR_PREFS_REGISTRATIONID = "AzureNotificationHub_registrationID";
55-
private static final String KEY_FOR_PREFS_CONNECTIONSTRING = "AzureNotificationHub_connectionString";
56-
private static final String KEY_FOR_PREFS_HUBNAME = "AzureNotificationHub_hubName";
57-
private static final String KEY_FOR_PREFS_FCMTOKEN = "AzureNotificationHub_FCMToken";
58-
private static final String KEY_FOR_PREFS_TAGS = "AzureNotificationHub_Tags";
59-
private static final String KEY_FOR_PREFS_SENDERID = "AzureNotificationHub_senderID";
60-
private static final String KEY_FOR_PREFS_CHANNELIMPORTANCE = "AzureNotificationHub_channelImportance";
61-
private static final String KEY_FOR_PREFS_CHANNELSHOWBADGE = "AzureNotificationHub_channelShowBadge";
62-
private static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights";
63-
private static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration";
54+
public static final String SHARED_PREFS_NAME = "com.azure.reactnative.notificationhub.NotificationHubUtil";
55+
public static final String KEY_FOR_PREFS_REGISTRATIONID = "AzureNotificationHub_registrationID";
56+
public static final String KEY_FOR_PREFS_CONNECTIONSTRING = "AzureNotificationHub_connectionString";
57+
public static final String KEY_FOR_PREFS_HUBNAME = "AzureNotificationHub_hubName";
58+
public static final String KEY_FOR_PREFS_FCMTOKEN = "AzureNotificationHub_FCMToken";
59+
public static final String KEY_FOR_PREFS_TAGS = "AzureNotificationHub_Tags";
60+
public static final String KEY_FOR_PREFS_SENDERID = "AzureNotificationHub_senderID";
61+
public static final String KEY_FOR_PREFS_CHANNELIMPORTANCE = "AzureNotificationHub_channelImportance";
62+
public static final String KEY_FOR_PREFS_CHANNELSHOWBADGE = "AzureNotificationHub_channelShowBadge";
63+
public static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights";
64+
public static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration";
6465

6566
private final ExecutorService mPool = Executors.newFixedThreadPool(1);
6667

6768
private boolean mIsForeground;
6869

70+
public static class IntentFactory {
71+
public static Intent createIntent() {
72+
return new Intent();
73+
}
74+
75+
public static Intent createIntent(String action) {
76+
return new Intent(action);
77+
}
78+
79+
public static Intent createIntent(Context context, Class intentClass) {
80+
return new Intent(context, intentClass);
81+
}
82+
}
83+
6984
public static NotificationHubUtil getInstance() {
7085
if (sharedNotificationHubUtilInstance == null) {
7186
sharedNotificationHubUtilInstance = new NotificationHubUtil();
@@ -202,7 +217,7 @@ public JSONObject convertBundleToJSON(Bundle bundle) {
202217
}
203218

204219
public Intent createBroadcastIntent(String action, JSONObject json) {
205-
Intent intent = new Intent(action);
220+
Intent intent = IntentFactory.createIntent(action);
206221
intent.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
207222
intent.putExtra("data", json.toString());
208223

@@ -216,7 +231,7 @@ public Class getMainActivityClass(Context context) {
216231
try {
217232
return Class.forName(className);
218233
} catch (ClassNotFoundException e) {
219-
e.printStackTrace();
234+
Log.e(TAG, ERROR_ACTIVITY_CLASS_NOT_FOUND, e);
220235
return null;
221236
}
222237
}
@@ -289,10 +304,8 @@ public Uri getSoundUri(Context context, Bundle bundle) {
289304
// So the strings 'my_sound.mp3' AND 'my_sound' are accepted
290305
// The reason is to make the iOS and android javascript interfaces compatible
291306

292-
int resId;
293-
if (context.getResources().getIdentifier(soundName, RESOURCE_DEF_TYPE_RAW, context.getPackageName()) != 0) {
294-
resId = context.getResources().getIdentifier(soundName, RESOURCE_DEF_TYPE_RAW, context.getPackageName());
295-
} else {
307+
int resId = context.getResources().getIdentifier(soundName, RESOURCE_DEF_TYPE_RAW, context.getPackageName());
308+
if (resId == 0) {
296309
soundName = soundName.substring(0, soundName.lastIndexOf('.'));
297310
resId = context.getResources().getIdentifier(soundName, RESOURCE_DEF_TYPE_RAW, context.getPackageName());
298311
}
@@ -305,7 +318,7 @@ public Uri getSoundUri(Context context, Bundle bundle) {
305318
}
306319

307320
public Intent createNotificationIntent(Context context, Bundle bundle, Class intentClass) {
308-
Intent intent = new Intent(context, intentClass);
321+
Intent intent = IntentFactory.createIntent(context, intentClass);
309322
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
310323
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, true);
311324
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, false);
@@ -340,7 +353,7 @@ public void processNotificationActions(Context context, Bundle bundle,
340353
continue;
341354
}
342355

343-
Intent actionIntent = new Intent();
356+
Intent actionIntent = IntentFactory.createIntent();
344357
actionIntent.setAction(context.getPackageName() + "." + action);
345358
// Add "action" for later identifying which button gets pressed.
346359
bundle.putString(KEY_REMOTE_NOTIFICATION_ACTION, action);

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ReactNativeFirebaseMessagingService extends FirebaseMessagingServic
2424
public static void createNotificationChannel(Context context) {
2525
if (notificationChannelID == null) {
2626
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
27-
NotificationChannelBuilder builder = new NotificationChannelBuilder();
27+
NotificationChannelBuilder builder = NotificationChannelBuilder.Factory.create();
2828
if (notificationHubUtil.hasChannelImportance(context)) {
2929
builder.setImportance(notificationHubUtil.getChannelImportance(context));
3030
}
@@ -52,11 +52,23 @@ public static void createNotificationChannel(Context context) {
5252
}
5353
}
5454

55+
public static void deleteNotificationChannel(Context context) {
56+
if (notificationChannelID != null) {
57+
final String channelToDeleteID = notificationChannelID;
58+
notificationChannelID = null;
59+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
60+
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
61+
Context.NOTIFICATION_SERVICE);
62+
notificationManager.deleteNotificationChannel(channelToDeleteID);
63+
}
64+
}
65+
}
66+
5567
@Override
5668
public void onNewToken(String token) {
5769
Log.i(TAG, "Refreshing FCM Registration Token");
5870

59-
Intent intent = new Intent(this, ReactNativeRegistrationIntentService.class);
71+
Intent intent = NotificationHubUtil.IntentFactory.createIntent(this, ReactNativeRegistrationIntentService.class);
6072
startService(intent);
6173
}
6274

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ public void register(ReadableMap config, Promise promise) {
153153
return;
154154
}
155155

156-
Intent intent = new Intent(reactContext, ReactNativeRegistrationIntentService.class);
156+
Intent intent = NotificationHubUtil.IntentFactory.createIntent(
157+
reactContext, ReactNativeRegistrationIntentService.class);
157158
reactContext.startService(intent);
158159
}
159160

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public final class ReactNativeNotificationsHandler {
7575
public static final String ERROR_COVERT_ACTIONS = "Exception while converting actions to JSON object.";
7676
public static final String ERROR_GET_ACTIONS_ARRAY = "Exception while getting action from actionsArray.";
7777
public static final String ERROR_SEND_PUSH_NOTIFICATION = "failed to send push notification";
78+
public static final String ERROR_ACTIVITY_CLASS_NOT_FOUND = "Activity class not found";
7879

7980
private static final long DEFAULT_VIBRATION = 300L;
8081

@@ -178,7 +179,7 @@ public static void sendNotification(Context context, Bundle bundle, String notif
178179
notificationBuilder.setSound(soundUri);
179180
}
180181

181-
if (bundle.containsKey(KEY_REMOTE_NOTIFICATION_ONGOING) || bundle.getBoolean(KEY_REMOTE_NOTIFICATION_ONGOING)) {
182+
if (bundle.containsKey(KEY_REMOTE_NOTIFICATION_ONGOING)) {
182183
notificationBuilder.setOngoing(bundle.getBoolean(KEY_REMOTE_NOTIFICATION_ONGOING));
183184
}
184185

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ReactNativeRegistrationIntentService() {
2828
@Override
2929
protected void onHandleIntent(Intent intent) {
3030
final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
31-
final Intent event = new Intent(TAG);
31+
final Intent event = NotificationHubUtil.IntentFactory.createIntent(TAG);
3232
final NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
3333
final String connectionString = notificationHubUtil.getConnectionString(this);
3434
final String hubName = notificationHubUtil.getHubName(this);

sample/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ dependencies {
193193
testImplementation "org.powermock:powermock-core:2.0.2"
194194
testImplementation "org.powermock:powermock-api-mockito2:2.0.2"
195195
testImplementation "org.powermock:powermock-module-junit4:2.0.2"
196+
testImplementation "org.json:json:20180130"
196197

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

0 commit comments

Comments
 (0)