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,51 +387,26 @@ public void userProvidedPrivacyConsent(Promise promise) {
389387 promise .resolve (OneSignal .userProvidedPrivacyConsent ());
390388 }
391389
392- @ ReactMethod
393- public void setExternalUserId (String externalId ) {
394- OneSignal .setExternalUserId (externalId );
395- }
396-
397- @ ReactMethod
398- public void removeExternalUserId () {
399- OneSignal .removeExternalUserId ();
400- }
401-
402- private void registerNotificationsReceivedNotification () {
403- IntentFilter intentFilter = new IntentFilter (NOTIFICATION_RECEIVED_INTENT_FILTER );
404- mReactContext .registerReceiver (new BroadcastReceiver () {
405- @ Override
406- public void onReceive (Context context , Intent intent ) {
407- notifyNotificationReceived (intent .getExtras ());
408- }
409- }, intentFilter );
410- }
411-
412- private void registerNotificationsOpenedNotification () {
413- IntentFilter intentFilter = new IntentFilter (NOTIFICATION_OPENED_INTENT_FILTER );
414- mReactContext .registerReceiver (new BroadcastReceiver () {
415- @ Override
416- public void onReceive (Context context , Intent intent ) {
417- notifyNotificationOpened (intent .getExtras ());
418- }
419- }, intentFilter );
390+ @ Override
391+ public void notificationReceived (OSNotification notification ) {
392+ this .sendEvent ("OneSignal-remoteNotificationReceived" , RNUtils .jsonToWritableMap (notification .toJSONObject ()));
420393 }
421394
422- private void notifyNotificationReceived (Bundle bundle ) {
423- try {
424- JSONObject jsonObject = new JSONObject (bundle .getString ("notification" ));
425- sendEvent ("OneSignal-remoteNotificationReceived" , RNUtils .jsonToWritableMap (jsonObject ));
426- } catch (Throwable t ) {
427- t .printStackTrace ();
395+ @ Override
396+ public void notificationOpened (OSNotificationOpenResult result ) {
397+ if (!this .setNotificationOpenedHandler ) {
398+ this .coldStartNotificationResult = result ;
399+ return ;
428400 }
401+ this .sendEvent ("OneSignal-remoteNotificationOpened" , RNUtils .jsonToWritableMap (result .toJSONObject ()));
429402 }
430403
431- private void notifyNotificationOpened ( Bundle bundle ) {
432- try {
433- JSONObject jsonObject = new JSONObject ( bundle . getString ( "result" )) ;
434- sendEvent ( "OneSignal-remoteNotificationOpened" , RNUtils . jsonToWritableMap ( jsonObject ));
435- } catch ( Throwable t ) {
436- 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 ;
437410 }
438411 }
439412
@@ -443,10 +416,7 @@ public String getName() {
443416 }
444417
445418 @ Override
446- public void onHostDestroy () {
447- OneSignal .removeNotificationOpenedHandler ();
448- OneSignal .removeNotificationReceivedHandler ();
449- }
419+ public void onHostDestroy () { }
450420
451421 @ Override
452422 public void onHostPause () {
0 commit comments