Skip to content

Commit a6552f0

Browse files
committed
Fallback to Context Initialization
• Changed the SDK so that if, during initialization, the initial activity does not exist, the SDK will use the application context instead
1 parent cfc914d commit a6552f0

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,37 +106,33 @@ private JSONObject jsonFromErrorMessageString(String errorMessage) throws JSONEx
106106
@ReactMethod
107107
public void init(String appId) {
108108
Activity activity = mReactApplicationContext.getCurrentActivity();
109-
110-
if (activity == null) {
111-
// in some cases, especially with react-native-navigation, it can take a while for the Activity to be created
112-
// if null, we should re-attempt initialization after 50 milliseconds.
113-
final String currentAppId = appId;
114-
Timer timer = new Timer();
115-
timer.schedule(new TimerTask() {
116-
@Override
117-
public void run() {
118-
init(currentAppId);
119-
}
120-
}, 50);
121-
return;
122-
}
123109

124110
if (oneSignalInitDone) {
125-
Log.w("onesignal", "The OneSignal SDK has already been initialized");
111+
Log.w("onesignal", "Already initialized the OneSignal React-Native SDK");
126112
return;
127113
}
128114

129115
oneSignalInitDone = true;
130-
131116

132117
OneSignal.sdkType = "react";
133118

134-
OneSignal.init(activity,
135-
null,
136-
appId,
137-
new NotificationOpenedHandler(mReactContext),
138-
new NotificationReceivedHandler(mReactContext)
139-
);
119+
if (activity == 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+
OneSignal.init(mReactApplicationContext.getApplicationContext(),
123+
null,
124+
appId,
125+
new NotificationOpenedHandler(mReactContext),
126+
new NotificationReceivedHandler(mReactContext)
127+
);
128+
} else {
129+
OneSignal.init(activity,
130+
null,
131+
appId,
132+
new NotificationOpenedHandler(mReactContext),
133+
new NotificationReceivedHandler(mReactContext)
134+
);
135+
}
140136
}
141137

142138
@ReactMethod

0 commit comments

Comments
 (0)