6
6
import android .content .Intent ;
7
7
import android .content .IntentFilter ;
8
8
import android .os .Bundle ;
9
+
9
10
import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
10
11
11
12
import com .facebook .react .bridge .LifecycleEventListener ;
26
27
import com .microsoft .windowsazure .notifications .NotificationsManager ;
27
28
28
29
public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
30
+ public static final String AZURE_NOTIFICATION_HUB_NAME = "AzureNotificationHub" ;
29
31
public static final String NOTIF_REGISTER_AZURE_HUB_EVENT = "azureNotificationHubRegistered" ;
30
32
public static final String NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT = "azureNotificationHubRegistrationError" ;
31
33
public static final String DEVICE_NOTIF_EVENT = "remoteNotificationReceived" ;
32
34
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
+
33
46
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000 ;
34
47
private static final int NOTIFICATION_DELAY_ON_START = 3000 ;
35
48
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 ;
43
51
44
52
public ReactNativeNotificationHubModule (ReactApplicationContext reactContext ) {
45
53
super (reactContext );
@@ -53,25 +61,28 @@ public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
53
61
54
62
@ Override
55
63
public String getName () {
56
- return "AzureNotificationHub" ;
64
+ return AZURE_NOTIFICATION_HUB_NAME ;
57
65
}
58
66
59
67
@ ReactMethod
60
68
public void register (ReadableMap config , Promise promise ) {
61
69
NotificationHubUtil notificationHubUtil = NotificationHubUtil .getInstance ();
62
70
String connectionString = config .getString ("connectionString" );
63
71
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 ;
65
74
}
66
75
67
76
String hubName = config .getString ("hubName" );
68
77
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 ;
70
80
}
71
81
72
82
String senderID = config .getString ("senderID" );
73
83
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 ;
75
86
}
76
87
77
88
String [] tags = null ;
@@ -97,9 +108,9 @@ public void register(ReadableMap config, Promise promise) {
97
108
getCurrentActivity (),
98
109
apiAvailability ,
99
110
resultCode ));
100
- promise .reject (ERROR_PLAY_SERVICES , "User must enable Google Play Services." );
111
+ promise .reject (ERROR_PLAY_SERVICES , ERROR_PLAY_SERVICES_DISABLED );
101
112
} 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 );
103
114
}
104
115
return ;
105
116
}
@@ -118,10 +129,11 @@ public void unregister(Promise promise) {
118
129
String registrationId = notificationHubUtil .getRegistrationID (reactContext );
119
130
120
131
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 ;
122
134
}
123
135
124
- NotificationHub hub = new NotificationHub (hubName , connectionString , reactContext );
136
+ NotificationHub hub = notificationHubUtil . createNotificationHub (hubName , connectionString , reactContext );
125
137
try {
126
138
hub .unregister ();
127
139
notificationHubUtil .setRegistrationID (reactContext , null );
@@ -144,10 +156,15 @@ public void onHostResume() {
144
156
}
145
157
}
146
158
}
159
+
147
160
@ Override
148
- public void onHostPause () {}
161
+ public void onHostPause () {
162
+ }
163
+
149
164
@ Override
150
- public void onHostDestroy () {}
165
+ public void onHostDestroy () {
166
+ }
167
+
151
168
public class LocalBroadcastReceiver extends BroadcastReceiver {
152
169
@ Override
153
170
public void onReceive (Context context , Intent intent ) {
0 commit comments