@@ -75,7 +75,6 @@ private String appIdFromManifest(ReactApplicationContext context) {
7575 // However it seems it is also to soon to call getCurrentActivity() from the reactContext as well.
7676 // This will normally succeed when onHostResume fires instead.
7777 private void initOneSignal () {
78-
7978 // Uncomment to debug init issues.
8079 // OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.ERROR);
8180
@@ -106,22 +105,28 @@ private JSONObject jsonFromErrorMessageString(String errorMessage) throws JSONEx
106105
107106 @ ReactMethod
108107 public void init (String appId ) {
109- Activity activity = getCurrentActivity ();
110- if (activity == null || oneSignalInitDone ) {
111- Log .e ("onesignal" , "Unable to initialize the OneSignal SDK because activity is null " + (activity == null ) + " or oneSignalInitDone" + oneSignalInitDone );
108+ Context context = mReactApplicationContext .getCurrentActivity ();
109+
110+ if (oneSignalInitDone ) {
111+ Log .e ("onesignal" , "Already initialized the OneSignal React-Native SDK" );
112112 return ;
113113 }
114114
115115 oneSignalInitDone = true ;
116-
117116
118117 OneSignal .sdkType = "react" ;
119118
120- OneSignal .init (activity ,
121- null ,
122- appId ,
123- new NotificationOpenedHandler (mReactContext ),
124- new NotificationReceivedHandler (mReactContext )
119+ if (context == null ) {
120+ // in some cases, especially when react-native-navigation is installed,
121+ // the activity can be null, so we can initialize with the context instead
122+ context = mReactApplicationContext .getApplicationContext ();
123+ }
124+
125+ OneSignal .init (context ,
126+ null ,
127+ appId ,
128+ new NotificationOpenedHandler (mReactContext ),
129+ new NotificationReceivedHandler (mReactContext )
125130 );
126131 }
127132
0 commit comments