Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix React Native Null Activity #572

Merged
merged 4 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def safeExtGet(prop, fallback) {
}

android {
compileSdkVersion safeExtGet('compileSdkVersion', 23)
buildToolsVersion safeExtGet('buildToolsVersion', '23.0.1')
compileSdkVersion safeExtGet('compileSdkVersion', 26)
buildToolsVersion safeExtGet('buildToolsVersion', '26.0.2')

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 16)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ private String appIdFromManifest(ReactApplicationContext context) {
// However it seems it is also to soon to call getCurrentActivity() from the reactContext as well.
// This will normally succeed when onHostResume fires instead.
private void initOneSignal() {

// Uncomment to debug init issues.
// OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.ERROR);

Expand Down Expand Up @@ -106,22 +105,28 @@ private JSONObject jsonFromErrorMessageString(String errorMessage) throws JSONEx

@ReactMethod
public void init(String appId) {
Activity activity = getCurrentActivity();
if (activity == null || oneSignalInitDone) {
Log.e("onesignal", "Unable to initialize the OneSignal SDK because activity is null " + (activity == null) + " or oneSignalInitDone" + oneSignalInitDone);
Context context = mReactApplicationContext.getCurrentActivity();

if (oneSignalInitDone) {
Log.e("onesignal", "Already initialized the OneSignal React-Native SDK");
return;
}

oneSignalInitDone = true;


OneSignal.sdkType = "react";

OneSignal.init(activity,
null,
appId,
new NotificationOpenedHandler(mReactContext),
new NotificationReceivedHandler(mReactContext)
if (context == null) {
// in some cases, especially when react-native-navigation is installed,
// the activity can be null, so we can initialize with the context instead
context = mReactApplicationContext.getApplicationContext();
}

OneSignal.init(context,
null,
appId,
new NotificationOpenedHandler(mReactContext),
new NotificationReceivedHandler(mReactContext)
);
}

Expand Down