@@ -34,51 +34,46 @@ of this software and associated documentation files (the "Software"), to deal
34
34
35
35
package com .geektime .rnonesignalandroid ;
36
36
37
- import java .util .Iterator ;
38
- import java .util .HashMap ;
39
-
40
37
import android .content .Context ;
38
+ import android .content .pm .ApplicationInfo ;
41
39
import android .os .Bundle ;
42
40
import android .util .Log ;
43
- import android .content .pm .ApplicationInfo ;
44
41
45
- import com .facebook .react .bridge .Promise ;
46
42
import com .facebook .react .bridge .Callback ;
47
- import com .facebook .react .bridge .ReactMethod ;
48
- import com .facebook .react .bridge .ReadableMap ;
49
- import com .facebook .react .bridge .ReactContext ;
50
- import com .facebook .react .bridge .ReadableArray ;
51
- import com .facebook .react .bridge .WritableNativeMap ;
52
43
import com .facebook .react .bridge .LifecycleEventListener ;
44
+ import com .facebook .react .bridge .Promise ;
53
45
import com .facebook .react .bridge .ReactApplicationContext ;
46
+ import com .facebook .react .bridge .ReactContext ;
54
47
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
48
+ import com .facebook .react .bridge .ReactMethod ;
49
+ import com .facebook .react .bridge .ReadableArray ;
50
+ import com .facebook .react .bridge .ReadableMap ;
55
51
import com .facebook .react .modules .core .DeviceEventManagerModule ;
56
-
57
- import com .onesignal .OSEmailSubscriptionStateChanges ;
58
- import com .onesignal .OneSignal ;
59
- import com .onesignal .OSOutcomeEvent ;
60
52
import com .onesignal .OSDeviceState ;
53
+ import com .onesignal .OSEmailSubscriptionObserver ;
54
+ import com .onesignal .OSEmailSubscriptionStateChanges ;
61
55
import com .onesignal .OSInAppMessageAction ;
62
56
import com .onesignal .OSNotification ;
63
- import com .onesignal .OSNotificationReceivedEvent ;
64
57
import com .onesignal .OSNotificationOpenedResult ;
65
- import com .onesignal .OneSignal .OutcomeCallback ;
58
+ import com .onesignal .OSNotificationReceivedEvent ;
59
+ import com .onesignal .OSOutcomeEvent ;
60
+ import com .onesignal .OSPermissionObserver ;
61
+ import com .onesignal .OSPermissionStateChanges ;
62
+ import com .onesignal .OSSMSSubscriptionObserver ;
63
+ import com .onesignal .OSSMSSubscriptionStateChanges ;
64
+ import com .onesignal .OSSubscriptionObserver ;
65
+ import com .onesignal .OSSubscriptionStateChanges ;
66
+ import com .onesignal .OneSignal ;
66
67
import com .onesignal .OneSignal .EmailUpdateError ;
67
-
68
68
import com .onesignal .OneSignal .EmailUpdateHandler ;
69
69
import com .onesignal .OneSignal .OSInAppMessageClickHandler ;
70
70
import com .onesignal .OneSignal .OSNotificationOpenedHandler ;
71
+ import com .onesignal .OneSignal .OutcomeCallback ;
71
72
72
- import com .onesignal .OSPermissionObserver ;
73
- import com .onesignal .OSSubscriptionObserver ;
74
- import com .onesignal .OSEmailSubscriptionObserver ;
75
-
76
- import com .onesignal .OSPermissionStateChanges ;
77
- import com .onesignal .OSSubscriptionStateChanges ;
78
-
79
- import org .json .JSONObject ;
80
- import org .json .JSONArray ;
81
73
import org .json .JSONException ;
74
+ import org .json .JSONObject ;
75
+
76
+ import java .util .HashMap ;
82
77
83
78
public class RNOneSignal extends ReactContextBaseJavaModule
84
79
implements
@@ -87,8 +82,8 @@ public class RNOneSignal extends ReactContextBaseJavaModule
87
82
OSNotificationOpenedHandler ,
88
83
OSEmailSubscriptionObserver ,
89
84
LifecycleEventListener ,
90
- OSInAppMessageClickHandler
91
- {
85
+ OSInAppMessageClickHandler ,
86
+ OSSMSSubscriptionObserver {
92
87
93
88
public static final String HIDDEN_MESSAGE_KEY = "hidden" ;
94
89
@@ -104,6 +99,7 @@ public class RNOneSignal extends ReactContextBaseJavaModule
104
99
private boolean hasSetInAppClickedHandler = false ;
105
100
private boolean hasSetSubscriptionObserver = false ;
106
101
private boolean hasSetEmailSubscriptionObserver = false ;
102
+ private boolean hasSetSMSSubscriptionObserver = false ;
107
103
private boolean hasSetPermissionObserver = false ;
108
104
109
105
// A native module is supposed to invoke its callback only once. It can, however, store the callback and invoke it later.
@@ -166,7 +162,7 @@ private void initOneSignal() {
166
162
Context context = mReactApplicationContext .getCurrentActivity ();
167
163
168
164
if (oneSignalInitDone ) {
169
- Log .e ("onesignal " , "Already initialized the OneSignal React-Native SDK" );
165
+ Log .e ("OneSignal " , "Already initialized the OneSignal React-Native SDK" );
170
166
return ;
171
167
}
172
168
@@ -191,22 +187,28 @@ public void setAppId(String appId) {
191
187
/* Observers */
192
188
@ Override
193
189
public void onOSPermissionChanged (OSPermissionStateChanges stateChanges ) {
194
- Log .i ("Onesignal " , "sending permission change event" );
190
+ Log .i ("OneSignal " , "sending permission change event" );
195
191
sendEvent ("OneSignal-permissionChanged" , RNUtils .jsonToWritableMap (stateChanges .toJSONObject ()));
196
192
}
197
193
198
194
@ Override
199
195
public void onOSSubscriptionChanged (OSSubscriptionStateChanges stateChanges ) {
200
- Log .i ("Onesignal " , "sending subscription change event" );
196
+ Log .i ("OneSignal " , "sending subscription change event" );
201
197
sendEvent ("OneSignal-subscriptionChanged" , RNUtils .jsonToWritableMap (stateChanges .toJSONObject ()));
202
198
}
203
199
204
200
@ Override
205
201
public void onOSEmailSubscriptionChanged (OSEmailSubscriptionStateChanges stateChanges ) {
206
- Log .i ("Onesignal " , "sending email subscription change event" );
202
+ Log .i ("OneSignal " , "sending email subscription change event" );
207
203
sendEvent ("OneSignal-emailSubscriptionChanged" , RNUtils .jsonToWritableMap (stateChanges .toJSONObject ()));
208
204
}
209
205
206
+ @ Override
207
+ public void onSMSSubscriptionChanged (OSSMSSubscriptionStateChanges stateChanges ) {
208
+ Log .i ("OneSignal" , "sending SMS subscription change event" );
209
+ sendEvent ("OneSignal-smsSubscriptionChanged" , RNUtils .jsonToWritableMap (stateChanges .toJSONObject ()));
210
+ }
211
+
210
212
@ ReactMethod
211
213
public void addPermissionObserver () {
212
214
if (!hasSetPermissionObserver ) {
@@ -231,11 +233,24 @@ public void addEmailSubscriptionObserver() {
231
233
}
232
234
}
233
235
236
+ @ ReactMethod
237
+ public void addSMSSubscriptionObserver () {
238
+ if (!hasSetSMSSubscriptionObserver ) {
239
+ OneSignal .addSMSSubscriptionObserver (this );
240
+ hasSetSMSSubscriptionObserver = true ;
241
+ }
242
+ }
243
+
234
244
/* Other methods */
235
245
236
246
@ ReactMethod
237
247
public void getDeviceState (Promise promise ) {
238
248
OSDeviceState state = OneSignal .getDeviceState ();
249
+ if (state == null ) {
250
+ Log .e ("OneSignal" , "getDeviceState: OSDeviceState is null" );
251
+ promise .reject ("Null OSDeviceState" , "OSDeviceState is null" );
252
+ return ;
253
+ }
239
254
promise .resolve (RNUtils .jsonToWritableMap (state .toJSONObject ()));
240
255
}
241
256
@@ -244,6 +259,11 @@ public void disablePush(boolean disable) {
244
259
OneSignal .disablePush (disable );
245
260
}
246
261
262
+ @ ReactMethod
263
+ public void unsubscribeWhenNotificationsAreDisabled (boolean unsubscribe ) {
264
+ OneSignal .unsubscribeWhenNotificationsAreDisabled (unsubscribe );
265
+ }
266
+
247
267
@ ReactMethod
248
268
public void sendTag (String key , String value ) {
249
269
OneSignal .sendTag (key , value );
@@ -313,6 +333,44 @@ public void onFailure(EmailUpdateError error) {
313
333
});
314
334
}
315
335
336
+ @ ReactMethod
337
+ public void setSMSNumber (String smsNumber , String smsAuthToken , final Callback callback ) {
338
+ OneSignal .setSMSNumber (smsNumber , smsAuthToken , new OneSignal .OSSMSUpdateHandler () {
339
+ @ Override
340
+ public void onSuccess (JSONObject result ) {
341
+ callback .invoke (RNUtils .jsonToWritableMap (result ));
342
+ }
343
+
344
+ @ Override
345
+ public void onFailure (OneSignal .OSSMSUpdateError error ) {
346
+ try {
347
+ callback .invoke (RNUtils .jsonToWritableMap (jsonFromErrorMessageString (error .getMessage ())));
348
+ } catch (JSONException exception ) {
349
+ exception .printStackTrace ();
350
+ }
351
+ }
352
+ });
353
+ }
354
+
355
+ @ ReactMethod
356
+ public void logoutSMSNumber (final Callback callback ) {
357
+ OneSignal .logoutSMSNumber (new OneSignal .OSSMSUpdateHandler () {
358
+ @ Override
359
+ public void onSuccess (JSONObject result ) {
360
+ callback .invoke (RNUtils .jsonToWritableMap (result ));
361
+ }
362
+
363
+ @ Override
364
+ public void onFailure (OneSignal .OSSMSUpdateError error ) {
365
+ try {
366
+ callback .invoke (RNUtils .jsonToWritableMap (jsonFromErrorMessageString (error .getMessage ())));
367
+ } catch (JSONException exception ) {
368
+ exception .printStackTrace ();
369
+ }
370
+ }
371
+ });
372
+ }
373
+
316
374
@ ReactMethod
317
375
public void promptLocation () {
318
376
OneSignal .promptLocation ();
@@ -323,6 +381,11 @@ public void setLogLevel(int logLevel, int visualLogLevel) {
323
381
OneSignal .setLogLevel (logLevel , visualLogLevel );
324
382
}
325
383
384
+ @ ReactMethod
385
+ public void isLocationShared (Promise promise ) {
386
+ promise .resolve (OneSignal .isLocationShared ());
387
+ }
388
+
326
389
@ ReactMethod
327
390
public void setLocationShared (Boolean shared ) {
328
391
OneSignal .setLocationShared (shared );
@@ -358,6 +421,16 @@ public void removeNotification(int id) {
358
421
OneSignal .removeNotification (id );
359
422
}
360
423
424
+ @ ReactMethod
425
+ public void removeGroupedNotifications (String id ) {
426
+ OneSignal .removeGroupedNotifications (id );
427
+ }
428
+
429
+ @ ReactMethod
430
+ public void requiresUserPrivacyConsent (Promise promise ) {
431
+ promise .resolve (OneSignal .requiresUserPrivacyConsent ());
432
+ }
433
+
361
434
@ ReactMethod
362
435
public void setRequiresUserPrivacyConsent (Boolean required ) {
363
436
OneSignal .setRequiresUserPrivacyConsent (required );
0 commit comments