@@ -75,7 +75,6 @@ private String appIdFromManifest(ReactApplicationContext context) {
75
75
// However it seems it is also to soon to call getCurrentActivity() from the reactContext as well.
76
76
// This will normally succeed when onHostResume fires instead.
77
77
private void initOneSignal () {
78
-
79
78
// Uncomment to debug init issues.
80
79
// OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.ERROR);
81
80
@@ -106,22 +105,28 @@ private JSONObject jsonFromErrorMessageString(String errorMessage) throws JSONEx
106
105
107
106
@ ReactMethod
108
107
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" );
112
112
return ;
113
113
}
114
114
115
115
oneSignalInitDone = true ;
116
-
117
116
118
117
OneSignal .sdkType = "react" ;
119
118
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 )
125
130
);
126
131
}
127
132
0 commit comments