Skip to content

Commit 3772950

Browse files
authored
Merge pull request #572 from geektimecoil/fix_react_native_navigation
Fix React Native Null Activity
2 parents a56b0fe + decba5a commit 3772950

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ def safeExtGet(prop, fallback) {
55
}
66

77
android {
8-
compileSdkVersion safeExtGet('compileSdkVersion', 23)
9-
buildToolsVersion safeExtGet('buildToolsVersion', '23.0.1')
8+
compileSdkVersion safeExtGet('compileSdkVersion', 26)
9+
buildToolsVersion safeExtGet('buildToolsVersion', '26.0.2')
1010

1111
defaultConfig {
1212
minSdkVersion safeExtGet('minSdkVersion', 16)

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)