55import android .app .Activity ;
66import android .content .BroadcastReceiver ;
77import android .content .Context ;
8- import android .content .Intent ;
9- import android .content .IntentFilter ;
108import android .os .Bundle ;
119import android .util .Log ;
1210import android .content .pm .ApplicationInfo ;
3230import com .onesignal .OneSignal .EmailUpdateHandler ;
3331import com .onesignal .OneSignal .EmailUpdateError ;
3432
33+
34+ import com .onesignal .OneSignal .NotificationOpenedHandler ;
35+ import com .onesignal .OneSignal .NotificationReceivedHandler ;
36+ import com .onesignal .OSNotificationOpenResult ;
37+ import com .onesignal .OSNotification ;
38+
3539import org .json .JSONObject ;
3640import org .json .JSONArray ;
3741import org .json .JSONException ;
4044/**
4145* Created by Avishay on 1/31/16.
4246*/
43- public class RNOneSignal extends ReactContextBaseJavaModule implements LifecycleEventListener {
44- public static final String NOTIFICATION_OPENED_INTENT_FILTER = "GTNotificationOpened" ;
45- public static final String NOTIFICATION_RECEIVED_INTENT_FILTER = "GTNotificationReceived" ;
47+ public class RNOneSignal extends ReactContextBaseJavaModule implements LifecycleEventListener , NotificationReceivedHandler , NotificationOpenedHandler {
4648 public static final String HIDDEN_MESSAGE_KEY = "hidden" ;
4749
4850 private ReactApplicationContext mReactApplicationContext ;
4951 private ReactContext mReactContext ;
5052 private boolean oneSignalInitDone ;
5153 private boolean registeredEvents = false ;
5254
55+ private OSNotificationOpenResult coldStartNotificationResult ;
56+ private boolean setNotificationOpenedHandler = false ;
57+ private boolean didSetRequiresPrivacyConsent = false ;
58+ private boolean waitingForUserPrivacyConsent = false ;
59+
5360 //ensure only one callback exists at a given time due to react-native restriction
5461 private Callback pendingGetTagsCallback ;
5562
@@ -81,19 +88,12 @@ private void initOneSignal() {
8188 // Uncomment to debug init issues.
8289 // OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.ERROR);
8390
84- if (!registeredEvents ) {
85- registeredEvents = true ;
86- registerNotificationsOpenedNotification ();
87- registerNotificationsReceivedNotification ();
88- }
89-
9091 OneSignal .sdkType = "react" ;
9192
9293 String appId = appIdFromManifest (mReactApplicationContext );
9394
94- if (appId != null && appId .length () > 0 ) {
95+ if (appId != null && appId .length () > 0 )
9596 init (appId );
96- }
9797 }
9898
9999 private void sendEvent (String eventName , Object params ) {
@@ -125,12 +125,10 @@ public void init(String appId) {
125125 context = mReactApplicationContext .getApplicationContext ();
126126 }
127127
128- OneSignal .init (context ,
129- null ,
130- appId ,
131- new NotificationOpenedHandler (mReactContext ),
132- new NotificationReceivedHandler (mReactContext )
133- );
128+ OneSignal .init (context , null , appId , this , this );
129+
130+ if (this .didSetRequiresPrivacyConsent )
131+ this .waitingForUserPrivacyConsent = true ;
134132 }
135133
136134 @ ReactMethod
@@ -389,41 +387,26 @@ public void userProvidedPrivacyConsent(Promise promise) {
389387 promise .resolve (OneSignal .userProvidedPrivacyConsent ());
390388 }
391389
392- private void registerNotificationsReceivedNotification () {
393- IntentFilter intentFilter = new IntentFilter (NOTIFICATION_RECEIVED_INTENT_FILTER );
394- mReactContext .registerReceiver (new BroadcastReceiver () {
395- @ Override
396- public void onReceive (Context context , Intent intent ) {
397- notifyNotificationReceived (intent .getExtras ());
398- }
399- }, intentFilter );
400- }
401-
402- private void registerNotificationsOpenedNotification () {
403- IntentFilter intentFilter = new IntentFilter (NOTIFICATION_OPENED_INTENT_FILTER );
404- mReactContext .registerReceiver (new BroadcastReceiver () {
405- @ Override
406- public void onReceive (Context context , Intent intent ) {
407- notifyNotificationOpened (intent .getExtras ());
408- }
409- }, intentFilter );
390+ @ Override
391+ public void notificationReceived (OSNotification notification ) {
392+ this .sendEvent ("OneSignal-remoteNotificationReceived" , RNUtils .jsonToWritableMap (notification .toJSONObject ()));
410393 }
411394
412- private void notifyNotificationReceived (Bundle bundle ) {
413- try {
414- JSONObject jsonObject = new JSONObject (bundle .getString ("notification" ));
415- sendEvent ("OneSignal-remoteNotificationReceived" , RNUtils .jsonToWritableMap (jsonObject ));
416- } catch (Throwable t ) {
417- t .printStackTrace ();
395+ @ Override
396+ public void notificationOpened (OSNotificationOpenResult result ) {
397+ if (!this .setNotificationOpenedHandler ) {
398+ this .coldStartNotificationResult = result ;
399+ return ;
418400 }
401+ this .sendEvent ("OneSignal-remoteNotificationOpened" , RNUtils .jsonToWritableMap (result .toJSONObject ()));
419402 }
420403
421- private void notifyNotificationOpened ( Bundle bundle ) {
422- try {
423- JSONObject jsonObject = new JSONObject ( bundle . getString ( "result" )) ;
424- sendEvent ( "OneSignal-remoteNotificationOpened" , RNUtils . jsonToWritableMap ( jsonObject ));
425- } catch ( Throwable t ) {
426- t . printStackTrace () ;
404+ @ ReactMethod
405+ public void didSetNotificationOpenedHandler () {
406+ this . setNotificationOpenedHandler = true ;
407+ if ( this . coldStartNotificationResult != null ) {
408+ this . notificationOpened ( this . coldStartNotificationResult );
409+ this . coldStartNotificationResult = null ;
427410 }
428411 }
429412
@@ -433,10 +416,7 @@ public String getName() {
433416 }
434417
435418 @ Override
436- public void onHostDestroy () {
437- OneSignal .removeNotificationOpenedHandler ();
438- OneSignal .removeNotificationReceivedHandler ();
439- }
419+ public void onHostDestroy () { }
440420
441421 @ Override
442422 public void onHostPause () {
0 commit comments