66import android .content .Intent ;
77import android .content .IntentFilter ;
88import android .os .Bundle ;
9+
910import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
1011
1112import com .facebook .react .bridge .LifecycleEventListener ;
2627import com .microsoft .windowsazure .notifications .NotificationsManager ;
2728
2829public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
30+ public static final String AZURE_NOTIFICATION_HUB_NAME = "AzureNotificationHub" ;
2931 public static final String NOTIF_REGISTER_AZURE_HUB_EVENT = "azureNotificationHubRegistered" ;
3032 public static final String NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT = "azureNotificationHubRegistrationError" ;
3133 public static final String DEVICE_NOTIF_EVENT = "remoteNotificationReceived" ;
3234
35+ public static final String ERROR_INVALID_ARGUMENTS = "E_INVALID_ARGUMENTS" ;
36+ public static final String ERROR_INVALID_CONNECTION_STRING = "Connection string cannot be null." ;
37+ public static final String ERROR_INVALID_HUBNAME = "Hub name cannot be null." ;
38+ public static final String ERROR_INVALID_SENDER_ID = "Sender ID cannot be null." ;
39+ public static final String ERROR_PLAY_SERVICES = "E_PLAY_SERVICES" ;
40+ public static final String ERROR_PLAY_SERVICES_DISABLED = "User must enable Google Play Services." ;
41+ public static final String ERROR_PLAY_SERVICES_UNSUPPORTED = "This device is not supported by Google Play Services." ;
42+ public static final String ERROR_NOTIFICATION_HUB = "E_NOTIFICATION_HUB" ;
43+ public static final String ERROR_NOT_REGISTERED = "E_NOT_REGISTERED" ;
44+ public static final String ERROR_NOT_REGISTERED_DESC = "No registration to Azure Notification Hub." ;
45+
3346 private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000 ;
3447 private static final int NOTIFICATION_DELAY_ON_START = 3000 ;
3548
36- private static final String ERROR_INVALID_ARGUMENTS = "E_INVALID_ARGUMENTS" ;
37- private static final String ERROR_PLAY_SERVICES = "E_PLAY_SERVICES" ;
38- private static final String ERROR_NOTIFICATION_HUB = "E_NOTIFICATION_HUB" ;
39- private static final String ERROR_NOT_REGISTERED = "E_NOT_REGISTERED" ;
40-
41- private ReactApplicationContext mReactContext ;
42- private LocalBroadcastReceiver mLocalBroadcastReceiver ;
49+ private ReactApplicationContext mReactContext ;
50+ private LocalBroadcastReceiver mLocalBroadcastReceiver ;
4351
4452 public ReactNativeNotificationHubModule (ReactApplicationContext reactContext ) {
4553 super (reactContext );
@@ -53,25 +61,28 @@ public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
5361
5462 @ Override
5563 public String getName () {
56- return "AzureNotificationHub" ;
64+ return AZURE_NOTIFICATION_HUB_NAME ;
5765 }
5866
5967 @ ReactMethod
6068 public void register (ReadableMap config , Promise promise ) {
6169 NotificationHubUtil notificationHubUtil = NotificationHubUtil .getInstance ();
6270 String connectionString = config .getString ("connectionString" );
6371 if (connectionString == null ) {
64- promise .reject (ERROR_INVALID_ARGUMENTS , "Connection string cannot be null." );
72+ promise .reject (ERROR_INVALID_ARGUMENTS , ERROR_INVALID_CONNECTION_STRING );
73+ return ;
6574 }
6675
6776 String hubName = config .getString ("hubName" );
6877 if (hubName == null ) {
69- promise .reject (ERROR_INVALID_ARGUMENTS , "Hub name cannot be null." );
78+ promise .reject (ERROR_INVALID_ARGUMENTS , ERROR_INVALID_HUBNAME );
79+ return ;
7080 }
7181
7282 String senderID = config .getString ("senderID" );
7383 if (senderID == null ) {
74- promise .reject (ERROR_INVALID_ARGUMENTS , "Sender ID cannot be null." );
84+ promise .reject (ERROR_INVALID_ARGUMENTS , ERROR_INVALID_SENDER_ID );
85+ return ;
7586 }
7687
7788 String [] tags = null ;
@@ -97,9 +108,9 @@ public void register(ReadableMap config, Promise promise) {
97108 getCurrentActivity (),
98109 apiAvailability ,
99110 resultCode ));
100- promise .reject (ERROR_PLAY_SERVICES , "User must enable Google Play Services." );
111+ promise .reject (ERROR_PLAY_SERVICES , ERROR_PLAY_SERVICES_DISABLED );
101112 } else {
102- promise .reject (ERROR_PLAY_SERVICES , "This device is not supported by Google Play Services." );
113+ promise .reject (ERROR_PLAY_SERVICES , ERROR_PLAY_SERVICES_UNSUPPORTED );
103114 }
104115 return ;
105116 }
@@ -118,10 +129,11 @@ public void unregister(Promise promise) {
118129 String registrationId = notificationHubUtil .getRegistrationID (reactContext );
119130
120131 if (connectionString == null || hubName == null || registrationId == null ) {
121- promise .reject (ERROR_NOT_REGISTERED , "No registration to Azure Notification Hub." );
132+ promise .reject (ERROR_NOT_REGISTERED , ERROR_NOT_REGISTERED_DESC );
133+ return ;
122134 }
123135
124- NotificationHub hub = new NotificationHub (hubName , connectionString , reactContext );
136+ NotificationHub hub = notificationHubUtil . createNotificationHub (hubName , connectionString , reactContext );
125137 try {
126138 hub .unregister ();
127139 notificationHubUtil .setRegistrationID (reactContext , null );
@@ -144,10 +156,15 @@ public void onHostResume() {
144156 }
145157 }
146158 }
159+
147160 @ Override
148- public void onHostPause () {}
161+ public void onHostPause () {
162+ }
163+
149164 @ Override
150- public void onHostDestroy () {}
165+ public void onHostDestroy () {
166+ }
167+
151168 public class LocalBroadcastReceiver extends BroadcastReceiver {
152169 @ Override
153170 public void onReceive (Context context , Intent intent ) {
0 commit comments