Skip to content

Commit c7bf7f8

Browse files
committed
Refactored constant strings.
1 parent f0e00fe commit c7bf7f8

11 files changed

+119
-153
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.azure.reactnative.notificationhub;
2+
3+
public final class Constants {
4+
// Notification
5+
public static final String AZURE_NOTIFICATION_HUB_NAME = "AzureNotificationHub";
6+
public static final String NOTIF_REGISTER_AZURE_HUB_EVENT = "azureNotificationHubRegistered";
7+
public static final String NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT = "azureNotificationHubRegistrationError";
8+
public static final String DEVICE_NOTIF_EVENT = "remoteNotificationReceived";
9+
public static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id";
10+
11+
// Shared prefs used in NotificationHubUtil
12+
public static final String SHARED_PREFS_NAME = "com.azure.reactnative.notificationhub.NotificationHubUtil";
13+
public static final String KEY_FOR_PREFS_REGISTRATIONID = "AzureNotificationHub_registrationID";
14+
public static final String KEY_FOR_PREFS_CONNECTIONSTRING = "AzureNotificationHub_connectionString";
15+
public static final String KEY_FOR_PREFS_HUBNAME = "AzureNotificationHub_hubName";
16+
public static final String KEY_FOR_PREFS_FCMTOKEN = "AzureNotificationHub_FCMToken";
17+
public static final String KEY_FOR_PREFS_TAGS = "AzureNotificationHub_Tags";
18+
public static final String KEY_FOR_PREFS_SENDERID = "AzureNotificationHub_senderID";
19+
public static final String KEY_FOR_PREFS_CHANNELIMPORTANCE = "AzureNotificationHub_channelImportance";
20+
public static final String KEY_FOR_PREFS_CHANNELSHOWBADGE = "AzureNotificationHub_channelShowBadge";
21+
public static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights";
22+
public static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration";
23+
24+
// Remote notification payload
25+
public static final String KEY_REMOTE_NOTIFICATION_MESSAGE = "message";
26+
public static final String KEY_REMOTE_NOTIFICATION_ID = "google.message_id";
27+
public static final String KEY_REMOTE_NOTIFICATION_TITLE = "title";
28+
public static final String KEY_REMOTE_NOTIFICATION_PRIORITY = "google.original_priority";
29+
public static final String KEY_REMOTE_NOTIFICATION_TICKER = "ticker";
30+
public static final String KEY_REMOTE_NOTIFICATION_AUTO_CANCEL = "autoCancel";
31+
public static final String KEY_REMOTE_NOTIFICATION_GROUP = "group";
32+
public static final String KEY_REMOTE_NOTIFICATION_LARGE_ICON = "largeIcon";
33+
public static final String KEY_REMOTE_NOTIFICATION_SUB_TEXT = "subText";
34+
public static final String KEY_REMOTE_NOTIFICATION_NUMBER = "number";
35+
public static final String KEY_REMOTE_NOTIFICATION_SMALL_ICON = "smallIcon";
36+
public static final String KEY_REMOTE_NOTIFICATION_BIG_TEXT = "bigText";
37+
public static final String KEY_REMOTE_NOTIFICATION_PLAY_SOUND = "playSound";
38+
public static final String KEY_REMOTE_NOTIFICATION_SOUND_NAME = "soundName";
39+
public static final String KEY_REMOTE_NOTIFICATION_ONGOING = "ongoing";
40+
public static final String KEY_REMOTE_NOTIFICATION_COLOR = "color";
41+
public static final String KEY_REMOTE_NOTIFICATION_VIBRATE = "vibrate";
42+
public static final String KEY_REMOTE_NOTIFICATION_VIBRATION = "vibration";
43+
public static final String KEY_REMOTE_NOTIFICATION_FOREGROUND = "foreground";
44+
public static final String KEY_REMOTE_NOTIFICATION_ACTIONS = "actions";
45+
public static final String KEY_REMOTE_NOTIFICATION_ACTION = "action";
46+
public static final String KEY_REMOTE_NOTIFICATION_TAG = "tag";
47+
public static final String KEY_REMOTE_NOTIFICATION_USER_INTERACTION = "userInteraction";
48+
public static final String KEY_REMOTE_NOTIFICATION_COLDSTART = "coldstart";
49+
50+
// Remote notification payload's priority
51+
public static final String REMOTE_NOTIFICATION_PRIORITY_MAX = "max";
52+
public static final String REMOTE_NOTIFICATION_PRIORITY_HIGH = "high";
53+
public static final String REMOTE_NOTIFICATION_PRIORITY_LOW = "low";
54+
public static final String REMOTE_NOTIFICATION_PRIORITY_MIN = "min";
55+
public static final String REMOTE_NOTIFICATION_PRIORITY_NORMAL = "normal";
56+
57+
// Intent payload
58+
public static final String KEY_INTENT_NOTIFICATION = "notification";
59+
60+
// Resources
61+
public static final String RESOURCE_DEF_TYPE_MIPMAP = "mipmap";
62+
public static final String RESOURCE_DEF_TYPE_RAW = "raw";
63+
public static final String RESOURCE_NAME_NOTIFICATION = "ic_notification";
64+
public static final String RESOURCE_NAME_LAUNCHER = "ic_launcher";
65+
66+
// Errors
67+
public static final String ERROR_NO_ACTIVITY_CLASS = "No activity class found for the notification";
68+
public static final String ERROR_NO_MESSAGE = "No message specified for the notification";
69+
public static final String ERROR_NO_NOTIF_ID = "No notification ID specified for the notification";
70+
public static final String ERROR_COVERT_ACTIONS = "Exception while converting actions to JSON object.";
71+
public static final String ERROR_GET_ACTIONS_ARRAY = "Exception while getting action from actionsArray.";
72+
public static final String ERROR_SEND_PUSH_NOTIFICATION = "failed to send push notification";
73+
public static final String ERROR_ACTIVITY_CLASS_NOT_FOUND = "Activity class not found";
74+
public static final String ERROR_INVALID_ARGUMENTS = "E_INVALID_ARGUMENTS";
75+
public static final String ERROR_INVALID_CONNECTION_STRING = "Connection string cannot be null.";
76+
public static final String ERROR_INVALID_HUBNAME = "Hub name cannot be null.";
77+
public static final String ERROR_INVALID_SENDER_ID = "Sender ID cannot be null.";
78+
public static final String ERROR_PLAY_SERVICES = "E_PLAY_SERVICES";
79+
public static final String ERROR_PLAY_SERVICES_DISABLED = "User must enable Google Play Services.";
80+
public static final String ERROR_PLAY_SERVICES_UNSUPPORTED = "This device is not supported by Google Play Services.";
81+
public static final String ERROR_NOTIFICATION_HUB = "E_NOTIFICATION_HUB";
82+
public static final String ERROR_NOT_REGISTERED = "E_NOT_REGISTERED";
83+
public static final String ERROR_NOT_REGISTERED_DESC = "No registration to Azure Notification Hub.";
84+
85+
private Constants() {
86+
}
87+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import android.app.NotificationManager;
55

66
public class NotificationChannelBuilder {
7-
private String mID = ReactNativeNotificationsHandler.NOTIFICATION_CHANNEL_ID;
7+
private String mID = Constants.NOTIFICATION_CHANNEL_ID;
88
private CharSequence mName = "rn-push-notification-channel-name";
99
private String mDesc = "rn-push-notification-channel-description";
1010
private int mImportance = NotificationManager.IMPORTANCE_DEFAULT;

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

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,13 @@
2525
import org.json.JSONException;
2626
import org.json.JSONObject;
2727

28-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.ERROR_ACTIVITY_CLASS_NOT_FOUND;
29-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.ERROR_COVERT_ACTIONS;
30-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.ERROR_GET_ACTIONS_ARRAY;
31-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_INTENT_NOTIFICATION;
32-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_ACTION;
33-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_ACTIONS;
34-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_COLDSTART;
35-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_FOREGROUND;
36-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_SMALL_ICON;
37-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_SOUND_NAME;
38-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_USER_INTERACTION;
39-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_HIGH;
40-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_LOW;
41-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_MAX;
42-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_MIN;
43-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_NORMAL;
44-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.RESOURCE_DEF_TYPE_MIPMAP;
45-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.RESOURCE_DEF_TYPE_RAW;
46-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.RESOURCE_NAME_LAUNCHER;
47-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.RESOURCE_NAME_NOTIFICATION;
28+
import static com.azure.reactnative.notificationhub.Constants.*;
4829

4930
public class NotificationHubUtil {
5031
public static final String TAG = "NotificationHubUtil";
5132

5233
private static NotificationHubUtil sharedNotificationHubUtilInstance = null;
5334

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";
65-
6635
private final ExecutorService mPool = Executors.newFixedThreadPool(1);
6736

6837
private boolean mIsForeground;
@@ -218,7 +187,7 @@ public JSONObject convertBundleToJSON(Bundle bundle) {
218187

219188
public Intent createBroadcastIntent(String action, JSONObject json) {
220189
Intent intent = IntentFactory.createIntent(action);
221-
intent.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
190+
intent.putExtra("event", DEVICE_NOTIF_EVENT);
222191
intent.putExtra("data", json.toString());
223192

224193
return intent;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
import com.google.firebase.messaging.FirebaseMessagingService;
1212
import com.google.firebase.messaging.RemoteMessage;
1313

14-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_COLDSTART;
15-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_FOREGROUND;
16-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_USER_INTERACTION;
14+
import static com.azure.reactnative.notificationhub.Constants.*;
1715

1816
public class ReactNativeFirebaseMessagingService extends FirebaseMessagingService {
1917

@@ -41,7 +39,7 @@ public static void createNotificationChannel(Context context) {
4139
builder.enableVibration(notificationHubUtil.getChannelEnableVibration(context));
4240
}
4341

44-
notificationChannelID = ReactNativeNotificationsHandler.NOTIFICATION_CHANNEL_ID;
42+
notificationChannelID = NOTIFICATION_CHANNEL_ID;
4543
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
4644
NotificationChannel channel = builder.build();
4745
NotificationManager notificationManager = (NotificationManager) context.getSystemService(

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,10 @@
2626
import com.facebook.react.bridge.ReadableMap;
2727
import com.facebook.react.bridge.UiThreadUtil;
2828

29-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_INTENT_NOTIFICATION;
30-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_COLDSTART;
31-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_FOREGROUND;
32-
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_USER_INTERACTION;
29+
import static com.azure.reactnative.notificationhub.Constants.*;
3330

3431
public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements
3532
ActivityEventListener, LifecycleEventListener {
36-
public static final String AZURE_NOTIFICATION_HUB_NAME = "AzureNotificationHub";
37-
public static final String NOTIF_REGISTER_AZURE_HUB_EVENT = "azureNotificationHubRegistered";
38-
public static final String NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT = "azureNotificationHubRegistrationError";
39-
public static final String DEVICE_NOTIF_EVENT = "remoteNotificationReceived";
40-
41-
public static final String ERROR_INVALID_ARGUMENTS = "E_INVALID_ARGUMENTS";
42-
public static final String ERROR_INVALID_CONNECTION_STRING = "Connection string cannot be null.";
43-
public static final String ERROR_INVALID_HUBNAME = "Hub name cannot be null.";
44-
public static final String ERROR_INVALID_SENDER_ID = "Sender ID cannot be null.";
45-
public static final String ERROR_PLAY_SERVICES = "E_PLAY_SERVICES";
46-
public static final String ERROR_PLAY_SERVICES_DISABLED = "User must enable Google Play Services.";
47-
public static final String ERROR_PLAY_SERVICES_UNSUPPORTED = "This device is not supported by Google Play Services.";
48-
public static final String ERROR_NOTIFICATION_HUB = "E_NOTIFICATION_HUB";
49-
public static final String ERROR_NOT_REGISTERED = "E_NOT_REGISTERED";
50-
public static final String ERROR_NOT_REGISTERED_DESC = "No registration to Azure Notification Hub.";
51-
5233
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
5334
private static final int NOTIFICATION_DELAY_ON_START = 3000;
5435

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

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,62 +21,11 @@
2121

2222
import org.json.JSONObject;
2323

24+
import static com.azure.reactnative.notificationhub.Constants.*;
25+
2426
public final class ReactNativeNotificationsHandler {
2527
public static final String TAG = "ReactNativeNotification";
2628

27-
public static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id";
28-
29-
// Remote notification payload
30-
public static final String KEY_REMOTE_NOTIFICATION_MESSAGE = "message";
31-
public static final String KEY_REMOTE_NOTIFICATION_ID = "google.message_id";
32-
public static final String KEY_REMOTE_NOTIFICATION_TITLE = "title";
33-
public static final String KEY_REMOTE_NOTIFICATION_PRIORITY = "google.original_priority";
34-
public static final String KEY_REMOTE_NOTIFICATION_TICKER = "ticker";
35-
public static final String KEY_REMOTE_NOTIFICATION_AUTO_CANCEL = "autoCancel";
36-
public static final String KEY_REMOTE_NOTIFICATION_GROUP = "group";
37-
public static final String KEY_REMOTE_NOTIFICATION_LARGE_ICON = "largeIcon";
38-
public static final String KEY_REMOTE_NOTIFICATION_SUB_TEXT = "subText";
39-
public static final String KEY_REMOTE_NOTIFICATION_NUMBER = "number";
40-
public static final String KEY_REMOTE_NOTIFICATION_SMALL_ICON = "smallIcon";
41-
public static final String KEY_REMOTE_NOTIFICATION_BIG_TEXT = "bigText";
42-
public static final String KEY_REMOTE_NOTIFICATION_PLAY_SOUND = "playSound";
43-
public static final String KEY_REMOTE_NOTIFICATION_SOUND_NAME = "soundName";
44-
public static final String KEY_REMOTE_NOTIFICATION_ONGOING = "ongoing";
45-
public static final String KEY_REMOTE_NOTIFICATION_COLOR = "color";
46-
public static final String KEY_REMOTE_NOTIFICATION_VIBRATE = "vibrate";
47-
public static final String KEY_REMOTE_NOTIFICATION_VIBRATION = "vibration";
48-
public static final String KEY_REMOTE_NOTIFICATION_FOREGROUND = "foreground";
49-
public static final String KEY_REMOTE_NOTIFICATION_ACTIONS = "actions";
50-
public static final String KEY_REMOTE_NOTIFICATION_ACTION = "action";
51-
public static final String KEY_REMOTE_NOTIFICATION_TAG = "tag";
52-
public static final String KEY_REMOTE_NOTIFICATION_USER_INTERACTION = "userInteraction";
53-
public static final String KEY_REMOTE_NOTIFICATION_COLDSTART = "coldstart";
54-
55-
// Remote notification payload's priority
56-
public static final String REMOTE_NOTIFICATION_PRIORITY_MAX = "max";
57-
public static final String REMOTE_NOTIFICATION_PRIORITY_HIGH = "high";
58-
public static final String REMOTE_NOTIFICATION_PRIORITY_LOW = "low";
59-
public static final String REMOTE_NOTIFICATION_PRIORITY_MIN = "min";
60-
public static final String REMOTE_NOTIFICATION_PRIORITY_NORMAL = "normal";
61-
62-
// Intent payload
63-
public static final String KEY_INTENT_NOTIFICATION = "notification";
64-
65-
// Resources
66-
public static final String RESOURCE_DEF_TYPE_MIPMAP = "mipmap";
67-
public static final String RESOURCE_DEF_TYPE_RAW = "raw";
68-
public static final String RESOURCE_NAME_NOTIFICATION = "ic_notification";
69-
public static final String RESOURCE_NAME_LAUNCHER = "ic_launcher";
70-
71-
// Errors
72-
public static final String ERROR_NO_ACTIVITY_CLASS = "No activity class found for the notification";
73-
public static final String ERROR_NO_MESSAGE = "No message specified for the notification";
74-
public static final String ERROR_NO_NOTIF_ID = "No notification ID specified for the notification";
75-
public static final String ERROR_COVERT_ACTIONS = "Exception while converting actions to JSON object.";
76-
public static final String ERROR_GET_ACTIONS_ARRAY = "Exception while getting action from actionsArray.";
77-
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";
79-
8029
private static final long DEFAULT_VIBRATION = 300L;
8130

8231
private ReactNativeNotificationsHandler() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void onSuccess(InstanceIdResult instanceIdResult) {
7171
notificationHubUtil.setRegistrationID(ReactNativeRegistrationIntentService.this, regID);
7272
notificationHubUtil.setFCMToken(ReactNativeRegistrationIntentService.this, token);
7373

74-
event.putExtra("event", ReactNativeNotificationHubModule.NOTIF_REGISTER_AZURE_HUB_EVENT);
74+
event.putExtra("event", Constants.NOTIF_REGISTER_AZURE_HUB_EVENT);
7575
event.putExtra("data", regID);
7676
localBroadcastManager.sendBroadcast(event);
7777

@@ -82,7 +82,7 @@ public void onSuccess(InstanceIdResult instanceIdResult) {
8282
} catch (Exception e) {
8383
Log.e(TAG, "Failed to complete token refresh", e);
8484

85-
event.putExtra("event", ReactNativeNotificationHubModule.NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT);
85+
event.putExtra("event", Constants.NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT);
8686
event.putExtra("data", e.getMessage());
8787
localBroadcastManager.sendBroadcast(event);
8888
}

0 commit comments

Comments
 (0)