7
7
import android .content .IntentFilter ;
8
8
import android .os .Bundle ;
9
9
10
+ import androidx .core .app .NotificationManagerCompat ;
10
11
import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
11
12
12
13
import com .facebook .react .bridge .ActivityEventListener ;
14
+ import com .facebook .react .bridge .Arguments ;
13
15
import com .facebook .react .bridge .LifecycleEventListener ;
16
+ import com .facebook .react .bridge .WritableMap ;
14
17
import com .google .android .gms .common .ConnectionResult ;
15
18
import com .google .android .gms .common .GoogleApiAvailability ;
16
19
@@ -61,6 +64,29 @@ public boolean getIsForeground() {
61
64
return notificationHubUtil .getAppIsForeground ();
62
65
}
63
66
67
+ @ ReactMethod
68
+ public void getInitialNotification (Promise promise ) {
69
+ Activity activity = getCurrentActivity ();
70
+ if (activity == null ) {
71
+ promise .reject (ERROR_GET_INIT_NOTIFICATION , ERROR_ACTIVITY_IS_NULL );
72
+ return ;
73
+ }
74
+
75
+ Intent intent = activity .getIntent ();
76
+ if (intent != null && intent .getAction () != null ) {
77
+ if (intent .getExtras () == null ) {
78
+ // In certain cases while app cold launches, i.getExtras() returns null.
79
+ // Adding the check to make sure app won't crash,
80
+ // and still successfully launches from notification
81
+ promise .reject (ERROR_GET_INIT_NOTIFICATION , ERROR_INTENT_EXTRAS_IS_NULL );
82
+ } else {
83
+ promise .resolve (ReactNativeUtil .convertBundleToMap (intent .getExtras ()));
84
+ }
85
+ } else {
86
+ promise .reject (ERROR_GET_INIT_NOTIFICATION , ERROR_ACTIVITY_INTENT_IS_NULL );
87
+ }
88
+ }
89
+
64
90
@ ReactMethod
65
91
public void register (ReadableMap config , Promise promise ) {
66
92
ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil .getInstance ();
@@ -95,6 +121,114 @@ public void register(ReadableMap config, Promise promise) {
95
121
notificationHubUtil .setConnectionString (reactContext , connectionString );
96
122
notificationHubUtil .setHubName (reactContext , hubName );
97
123
notificationHubUtil .setSenderID (reactContext , senderID );
124
+ notificationHubUtil .setTemplated (reactContext , false );
125
+ notificationHubUtil .setTags (reactContext , tags );
126
+
127
+ if (config .hasKey (KEY_REGISTRATION_CHANNELNAME )) {
128
+ String channelName = config .getString (KEY_REGISTRATION_CHANNELNAME );
129
+ notificationHubUtil .setChannelName (reactContext , channelName );
130
+ }
131
+
132
+ if (config .hasKey (KEY_REGISTRATION_CHANNELIMPORTANCE )) {
133
+ int channelImportance = config .getInt (KEY_REGISTRATION_CHANNELIMPORTANCE );
134
+ notificationHubUtil .setChannelImportance (reactContext , channelImportance );
135
+ }
136
+
137
+ if (config .hasKey (KEY_REGISTRATION_CHANNELSHOWBADGE )) {
138
+ boolean channelShowBadge = config .getBoolean (KEY_REGISTRATION_CHANNELSHOWBADGE );
139
+ notificationHubUtil .setChannelShowBadge (reactContext , channelShowBadge );
140
+ }
141
+
142
+ if (config .hasKey (KEY_REGISTRATION_CHANNELENABLELIGHTS )) {
143
+ boolean channelEnableLights = config .getBoolean (KEY_REGISTRATION_CHANNELENABLELIGHTS );
144
+ notificationHubUtil .setChannelEnableLights (reactContext , channelEnableLights );
145
+ }
146
+
147
+ if (config .hasKey (KEY_REGISTRATION_CHANNELENABLEVIBRATION )) {
148
+ boolean channelEnableVibration = config .getBoolean (KEY_REGISTRATION_CHANNELENABLEVIBRATION );
149
+ notificationHubUtil .setChannelEnableVibration (reactContext , channelEnableVibration );
150
+ }
151
+
152
+ String uuid = notificationHubUtil .getUUID (reactContext );
153
+ if (uuid == null ) {
154
+ uuid = ReactNativeUtil .genUUID ();
155
+ notificationHubUtil .setUUID (reactContext , uuid );
156
+ }
157
+
158
+ GoogleApiAvailability apiAvailability = GoogleApiAvailability .getInstance ();
159
+ int resultCode = apiAvailability .isGooglePlayServicesAvailable (reactContext );
160
+ if (resultCode != ConnectionResult .SUCCESS ) {
161
+ if (apiAvailability .isUserResolvableError (resultCode )) {
162
+ UiThreadUtil .runOnUiThread (
163
+ new GoogleApiAvailabilityRunnable (
164
+ getCurrentActivity (),
165
+ apiAvailability ,
166
+ resultCode ));
167
+ promise .reject (ERROR_PLAY_SERVICES , ERROR_PLAY_SERVICES_DISABLED );
168
+ } else {
169
+ promise .reject (ERROR_PLAY_SERVICES , ERROR_PLAY_SERVICES_UNSUPPORTED );
170
+ }
171
+ return ;
172
+ }
173
+
174
+ Intent intent = ReactNativeNotificationHubUtil .IntentFactory .createIntent (
175
+ reactContext , ReactNativeRegistrationIntentService .class );
176
+ ReactNativeRegistrationIntentService .enqueueWork (reactContext , intent );
177
+
178
+ WritableMap res = Arguments .createMap ();
179
+ res .putString (KEY_PROMISE_RESOLVE_UUID , uuid );
180
+ promise .resolve (res );
181
+ }
182
+
183
+ @ ReactMethod
184
+ public void registerTemplate (ReadableMap config , Promise promise ) {
185
+ ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil .getInstance ();
186
+ String connectionString = config .getString (KEY_REGISTRATION_CONNECTIONSTRING );
187
+ if (connectionString == null ) {
188
+ promise .reject (ERROR_INVALID_ARGUMENTS , ERROR_INVALID_CONNECTION_STRING );
189
+ return ;
190
+ }
191
+
192
+ String hubName = config .getString (KEY_REGISTRATION_HUBNAME );
193
+ if (hubName == null ) {
194
+ promise .reject (ERROR_INVALID_ARGUMENTS , ERROR_INVALID_HUBNAME );
195
+ return ;
196
+ }
197
+
198
+ String senderID = config .getString (KEY_REGISTRATION_SENDERID );
199
+ if (senderID == null ) {
200
+ promise .reject (ERROR_INVALID_ARGUMENTS , ERROR_INVALID_SENDER_ID );
201
+ return ;
202
+ }
203
+
204
+ String templateName = config .getString (KEY_REGISTRATION_TEMPLATENAME );
205
+ if (templateName == null ) {
206
+ promise .reject (ERROR_INVALID_ARGUMENTS , ERROR_INVALID_TEMPLATE_NAME );
207
+ return ;
208
+ }
209
+
210
+ String template = config .getString (KEY_REGISTRATION_TEMPLATE );
211
+ if (template == null ) {
212
+ promise .reject (ERROR_INVALID_ARGUMENTS , ERROR_INVALID_TEMPLATE );
213
+ return ;
214
+ }
215
+
216
+ String [] tags = null ;
217
+ if (config .hasKey (KEY_REGISTRATION_TAGS ) && !config .isNull (KEY_REGISTRATION_TAGS )) {
218
+ ReadableArray tagsJson = config .getArray (KEY_REGISTRATION_TAGS );
219
+ tags = new String [tagsJson .size ()];
220
+ for (int i = 0 ; i < tagsJson .size (); ++i ) {
221
+ tags [i ] = tagsJson .getString (i );
222
+ }
223
+ }
224
+
225
+ ReactContext reactContext = getReactApplicationContext ();
226
+ notificationHubUtil .setConnectionString (reactContext , connectionString );
227
+ notificationHubUtil .setHubName (reactContext , hubName );
228
+ notificationHubUtil .setSenderID (reactContext , senderID );
229
+ notificationHubUtil .setTemplateName (reactContext , templateName );
230
+ notificationHubUtil .setTemplate (reactContext , template );
231
+ notificationHubUtil .setTemplated (reactContext , true );
98
232
notificationHubUtil .setTags (reactContext , tags );
99
233
100
234
if (config .hasKey (KEY_REGISTRATION_CHANNELNAME )) {
@@ -122,6 +256,12 @@ public void register(ReadableMap config, Promise promise) {
122
256
notificationHubUtil .setChannelEnableVibration (reactContext , channelEnableVibration );
123
257
}
124
258
259
+ String uuid = notificationHubUtil .getUUID (reactContext );
260
+ if (uuid == null ) {
261
+ uuid = ReactNativeUtil .genUUID ();
262
+ notificationHubUtil .setUUID (reactContext , uuid );
263
+ }
264
+
125
265
GoogleApiAvailability apiAvailability = GoogleApiAvailability .getInstance ();
126
266
int resultCode = apiAvailability .isGooglePlayServicesAvailable (reactContext );
127
267
if (resultCode != ConnectionResult .SUCCESS ) {
@@ -141,6 +281,10 @@ public void register(ReadableMap config, Promise promise) {
141
281
Intent intent = ReactNativeNotificationHubUtil .IntentFactory .createIntent (
142
282
reactContext , ReactNativeRegistrationIntentService .class );
143
283
ReactNativeRegistrationIntentService .enqueueWork (reactContext , intent );
284
+
285
+ WritableMap res = Arguments .createMap ();
286
+ res .putString (KEY_PROMISE_RESOLVE_UUID , uuid );
287
+ promise .resolve (res );
144
288
}
145
289
146
290
@ ReactMethod
@@ -161,11 +305,63 @@ public void unregister(Promise promise) {
161
305
try {
162
306
hub .unregister ();
163
307
notificationHubUtil .setRegistrationID (reactContext , null );
308
+ notificationHubUtil .setUUID (reactContext , null );
309
+ promise .resolve (AZURE_NOTIFICATION_HUB_UNREGISTERED );
164
310
} catch (Exception e ) {
165
311
promise .reject (ERROR_NOTIFICATION_HUB , e );
166
312
}
167
313
}
168
314
315
+ @ ReactMethod
316
+ public void unregisterTemplate (String templateName , Promise promise ) {
317
+ ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil .getInstance ();
318
+
319
+ ReactContext reactContext = getReactApplicationContext ();
320
+ String connectionString = notificationHubUtil .getConnectionString (reactContext );
321
+ String hubName = notificationHubUtil .getHubName (reactContext );
322
+ String registrationId = notificationHubUtil .getRegistrationID (reactContext );
323
+
324
+ if (connectionString == null || hubName == null || registrationId == null ) {
325
+ promise .reject (ERROR_NOT_REGISTERED , ERROR_NOT_REGISTERED_DESC );
326
+ return ;
327
+ }
328
+
329
+ NotificationHub hub = ReactNativeUtil .createNotificationHub (hubName , connectionString , reactContext );
330
+ try {
331
+ hub .unregisterTemplate (templateName );
332
+ notificationHubUtil .setRegistrationID (reactContext , null );
333
+ notificationHubUtil .setUUID (reactContext , null );
334
+ promise .resolve (AZURE_NOTIFICATION_HUB_UNREGISTERED );
335
+ } catch (Exception e ) {
336
+ promise .reject (ERROR_NOTIFICATION_HUB , e );
337
+ }
338
+ }
339
+
340
+ @ ReactMethod
341
+ public void getUUID (Boolean autoGen , Promise promise ) {
342
+ ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil .getInstance ();
343
+ ReactContext reactContext = getReactApplicationContext ();
344
+ String uuid = notificationHubUtil .getUUID (reactContext );
345
+
346
+ if (uuid != null ) {
347
+ promise .resolve (uuid );
348
+ } else if (autoGen ) {
349
+ uuid = ReactNativeUtil .genUUID ();
350
+ notificationHubUtil .setUUID (reactContext , uuid );
351
+ promise .resolve (uuid );
352
+ } else {
353
+ promise .reject (ERROR_GET_UUID , ERROR_NO_UUID_SET );
354
+ }
355
+ }
356
+
357
+ @ ReactMethod
358
+ public void isNotificationEnabledOnOSLevel (Promise promise ) {
359
+ ReactContext reactContext = getReactApplicationContext ();
360
+ NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat .from (reactContext );
361
+ boolean areNotificationsEnabled = notificationManagerCompat .areNotificationsEnabled ();
362
+ promise .resolve (areNotificationsEnabled );
363
+ }
364
+
169
365
@ Override
170
366
public void onHostResume () {
171
367
setIsForeground (true );
0 commit comments