Skip to content

Commit 306ea6b

Browse files
imSzukalaclaude
andauthored
fix(android): use ReactApplicationContext instead of getCurrentActivity in initialize and sendTokenToIntercom (#432)
getCurrentActivity() can return null when JS execution starts before the Activity is fully attached, causing intermittent "Activity is null" promise rejections. ReactApplicationContext is always available when a @ReactMethod is invoked, so getApplicationContext() on it is the correct way to obtain an Application reference. Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 3a0d01b commit 306ea6b

2 files changed

Lines changed: 14 additions & 35 deletions

File tree

android/src/newarch/IntercomModule.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,10 @@ public void sendTokenToIntercom(@NonNull String token, Promise promise) {
120120
promise.reject(IntercomErrorCodes.SEND_TOKEN_TO_INTERCOM, "token is null or empty");
121121
return;
122122
}
123-
Activity activity = getCurrentActivity();
124-
if (activity != null && activity.getApplication() != null) {
125-
intercomPushClient.sendTokenToIntercom(activity.getApplication(), token);
126-
Log.d(NAME, "sendTokenToIntercom");
127-
promise.resolve(true);
128-
} else {
129-
Log.e(NAME, "sendTokenToIntercom");
130-
Log.e(NAME, "no current activity");
131-
promise.reject(IntercomErrorCodes.SEND_TOKEN_TO_INTERCOM, "no current activity");
132-
}
133-
123+
Application application = (Application) getReactApplicationContext().getApplicationContext();
124+
intercomPushClient.sendTokenToIntercom(application, token);
125+
Log.d(NAME, "sendTokenToIntercom");
126+
promise.resolve(true);
134127
} catch (Exception err) {
135128
Log.e(NAME, "sendTokenToIntercom error:");
136129
Log.e(NAME, err.toString());
@@ -616,13 +609,9 @@ public void onFailure(@NonNull IntercomError intercomError) {
616609
@ReactMethod
617610
public void initialize(String apiKey, String appId, Promise promise) {
618611
try {
619-
Activity activity = getCurrentActivity();
620-
if (activity != null && activity.getApplication() != null) {
621-
IntercomModule.initialize(activity.getApplication(), apiKey, appId);
622-
promise.resolve(true);
623-
} else {
624-
promise.reject(IntercomErrorCodes.INITIALIZE_ERROR, "Activity is null");
625-
}
612+
Application application = (Application) getReactApplicationContext().getApplicationContext();
613+
IntercomModule.initialize(application, apiKey, appId);
614+
promise.resolve(true);
626615
} catch (Exception err) {
627616
Log.e(NAME, "initialize error:");
628617
Log.e(NAME, err.toString());

android/src/oldarch/IntercomModule.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,10 @@ public static void sendTokenToIntercom(Application application, @NonNull String
9898
@ReactMethod
9999
public void sendTokenToIntercom(@NonNull String token, Promise promise) {
100100
try {
101-
Activity activity = getCurrentActivity();
102-
if (activity != null) {
103-
intercomPushClient.sendTokenToIntercom(activity.getApplication(), token);
104-
Log.d(NAME, "sendTokenToIntercom");
105-
promise.resolve(true);
106-
} else {
107-
Log.e(NAME, "sendTokenToIntercom");
108-
Log.e(NAME, "no current activity");
109-
}
110-
101+
Application application = (Application) getReactApplicationContext().getApplicationContext();
102+
intercomPushClient.sendTokenToIntercom(application, token);
103+
Log.d(NAME, "sendTokenToIntercom");
104+
promise.resolve(true);
111105
} catch (Exception err) {
112106
Log.e(NAME, "sendTokenToIntercom error:");
113107
Log.e(NAME, err.toString());
@@ -593,13 +587,9 @@ public void onFailure(@NonNull IntercomError intercomError) {
593587
@ReactMethod
594588
public void initialize(String apiKey, String appId, Promise promise) {
595589
try {
596-
Activity activity = getCurrentActivity();
597-
if (activity != null && activity.getApplication() != null) {
598-
IntercomModule.initialize(activity.getApplication(), apiKey, appId);
599-
promise.resolve(true);
600-
} else {
601-
promise.reject(IntercomErrorCodes.INITIALIZE_ERROR, "Activity is null");
602-
}
590+
Application application = (Application) getReactApplicationContext().getApplicationContext();
591+
IntercomModule.initialize(application, apiKey, appId);
592+
promise.resolve(true);
603593
} catch (Exception err) {
604594
Log.e(NAME, "initialize error:");
605595
Log.e(NAME, err.toString());

0 commit comments

Comments
 (0)