Skip to content

Commit d17ba25

Browse files
committed
Added unit tests for notification handler class.
1 parent 8133b0d commit d17ba25

File tree

3 files changed

+425
-36
lines changed

3 files changed

+425
-36
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.RESOURCE_NAME_NOTIFICATION;
4747

4848
public class NotificationHubUtil {
49+
public static final String TAG = "NotificationHubUtil";
50+
4951
private static NotificationHubUtil sharedNotificationHubUtilInstance = null;
5052

5153
private static final String SHARED_PREFS_NAME = "com.azure.reactnative.notificationhub.NotificationHubUtil";
@@ -316,13 +318,12 @@ public Intent createNotificationIntent(Context context, Bundle bundle, Class int
316318
public void processNotificationActions(Context context, Bundle bundle,
317319
NotificationCompat.Builder notification,
318320
int notificationID) {
319-
final String tag = ReactNativeNotificationsHandler.TAG;
320321
JSONArray actionsArray = null;
321322
try {
322323
actionsArray = bundle.getString(KEY_REMOTE_NOTIFICATION_ACTIONS) != null ?
323324
new JSONArray(bundle.getString(KEY_REMOTE_NOTIFICATION_ACTIONS)) : null;
324325
} catch (JSONException e) {
325-
Log.e(tag, ERROR_COVERT_ACTIONS, e);
326+
Log.e(TAG, ERROR_COVERT_ACTIONS, e);
326327
}
327328

328329
if (actionsArray != null) {
@@ -335,7 +336,7 @@ public void processNotificationActions(Context context, Bundle bundle,
335336
try {
336337
action = actionsArray.getString(i);
337338
} catch (JSONException e) {
338-
Log.e(tag, ERROR_GET_ACTIONS_ARRAY, e);
339+
Log.e(TAG, ERROR_GET_ACTIONS_ARRAY, e);
339340
continue;
340341
}
341342

@@ -351,6 +352,23 @@ public void processNotificationActions(Context context, Bundle bundle,
351352
}
352353
}
353354

355+
public NotificationCompat.Builder initNotificationCompatBuilder(Context context,
356+
String notificationChannelID,
357+
String title,
358+
CharSequence ticker,
359+
int visibility,
360+
int priority,
361+
boolean autoCancel) {
362+
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, notificationChannelID)
363+
.setContentTitle(title)
364+
.setTicker(ticker)
365+
.setVisibility(visibility)
366+
.setPriority(priority)
367+
.setAutoCancel(autoCancel);
368+
369+
return notificationBuilder;
370+
}
371+
354372
private String getPref(Context context, String key) {
355373
SharedPreferences prefs =
356374
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import android.util.Log;
2121

22-
import org.json.JSONArray;
23-
import org.json.JSONException;
2422
import org.json.JSONObject;
2523

2624
public final class ReactNativeNotificationsHandler {
@@ -129,12 +127,14 @@ public static void sendNotification(Context context, Bundle bundle, String notif
129127
}
130128

131129
int priority = hubUtil.getNotificationCompatPriority(bundle.getString(KEY_REMOTE_NOTIFICATION_PRIORITY));
132-
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, notificationChannelID)
133-
.setContentTitle(title)
134-
.setTicker(bundle.getString(KEY_REMOTE_NOTIFICATION_TICKER))
135-
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
136-
.setPriority(priority)
137-
.setAutoCancel(bundle.getBoolean(KEY_REMOTE_NOTIFICATION_AUTO_CANCEL, true));
130+
NotificationCompat.Builder notificationBuilder = hubUtil.initNotificationCompatBuilder(
131+
context,
132+
notificationChannelID,
133+
title,
134+
bundle.getString(KEY_REMOTE_NOTIFICATION_TICKER),
135+
NotificationCompat.VISIBILITY_PRIVATE,
136+
priority,
137+
bundle.getBoolean(KEY_REMOTE_NOTIFICATION_AUTO_CANCEL, true));
138138

139139
String group = bundle.getString(KEY_REMOTE_NOTIFICATION_GROUP);
140140
if (group != null) {

0 commit comments

Comments
 (0)