From be236ba46d8ca0111242782c4792d69096ae003f Mon Sep 17 00:00:00 2001 From: Vlad M Date: Mon, 28 Jul 2025 22:07:51 +0100 Subject: [PATCH 1/2] Add manual sdk init option --- .../reactnative/IntercomErrorCodes.java | 1 + .../intercom/reactnative/IntercomModule.java | 917 +++++++++--------- ios/IntercomModule.m | 15 + src/expo-plugins/@types.ts | 1 + src/expo-plugins/index.ts | 46 +- src/index.tsx | 11 + 6 files changed, 522 insertions(+), 469 deletions(-) diff --git a/android/src/main/java/com/intercom/reactnative/IntercomErrorCodes.java b/android/src/main/java/com/intercom/reactnative/IntercomErrorCodes.java index b2114bd4..09d2af3e 100644 --- a/android/src/main/java/com/intercom/reactnative/IntercomErrorCodes.java +++ b/android/src/main/java/com/intercom/reactnative/IntercomErrorCodes.java @@ -10,6 +10,7 @@ public class IntercomErrorCodes { public static final String SET_LOG_LEVEL = "107"; public static final String GET_UNREAD_CONVERSATION = "108"; public static final String SET_USER_JWT = "109"; + public static final String INITIALIZE_ERROR = "110"; public static final String DISPLAY_MESSENGER = "201"; public static final String DISPLAY_MESSENGER_COMPOSER = "202"; public static final String DISPLAY_CONTENT = "203"; diff --git a/android/src/main/java/com/intercom/reactnative/IntercomModule.java b/android/src/main/java/com/intercom/reactnative/IntercomModule.java index a18736b5..91cd872c 100644 --- a/android/src/main/java/com/intercom/reactnative/IntercomModule.java +++ b/android/src/main/java/com/intercom/reactnative/IntercomModule.java @@ -40,496 +40,515 @@ @ReactModule(name = IntercomModule.NAME) public class IntercomModule extends ReactContextBaseJavaModule { - public static final String NAME = "IntercomModule"; - - private static final IntercomPushClient intercomPushClient = new IntercomPushClient(); - - public IntercomModule(ReactApplicationContext reactContext) { - super(reactContext); - } - - @Override - @NonNull - public String getName() { - return NAME; - } - - public static boolean isIntercomPush(RemoteMessage remoteMessage) { - try { - Map message = remoteMessage.getData(); - return intercomPushClient.isIntercomPush(message); - } catch (Exception err) { - Log.e(NAME, "isIntercomPush error:"); - Log.e(NAME, err.toString()); - return false; + + public static final String NAME = "IntercomModule"; + + private static final IntercomPushClient intercomPushClient = new IntercomPushClient(); + + public IntercomModule(ReactApplicationContext reactContext) { + super(reactContext); } - } - public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage - remoteMessage) { - try { - Map message = remoteMessage.getData(); - intercomPushClient.handlePush(application, message); - } catch (Exception err) { - Log.e(NAME, "handleRemotePushMessage error:"); - Log.e(NAME, err.toString()); + @Override + @NonNull + public String getName() { + return NAME; + } + + public static boolean isIntercomPush(RemoteMessage remoteMessage) { + try { + Map message = remoteMessage.getData(); + return intercomPushClient.isIntercomPush(message); + } catch (Exception err) { + Log.e(NAME, "isIntercomPush error:"); + Log.e(NAME, err.toString()); + return false; + } } - } - - public static void sendTokenToIntercom(Application application, @NonNull String token) { - intercomPushClient.sendTokenToIntercom(application, token); - Log.d(NAME, "sendTokenToIntercom"); - } - - @ReactMethod - public void handlePushMessage(Promise promise) { - try { - Intercom.client().handlePushMessage(); - promise.resolve(true); - Log.d(NAME, "handlePushMessage"); - } catch (Exception err) { - Log.e(NAME, "handlePushMessage error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.HANDLE_PUSH_MESSAGE, err.toString()); + + public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage + remoteMessage) { + try { + Map message = remoteMessage.getData(); + intercomPushClient.handlePush(application, message); + } catch (Exception err) { + Log.e(NAME, "handleRemotePushMessage error:"); + Log.e(NAME, err.toString()); + } } - } - - @ReactMethod - public void sendTokenToIntercom(@NonNull String token, Promise promise) { - try { - Activity activity = getCurrentActivity(); - if (activity != null) { - intercomPushClient.sendTokenToIntercom(activity.getApplication(), token); + + public static void sendTokenToIntercom(Application application, @NonNull String token) { + intercomPushClient.sendTokenToIntercom(application, token); Log.d(NAME, "sendTokenToIntercom"); - promise.resolve(true); - } else { - Log.e(NAME, "sendTokenToIntercom"); - Log.e(NAME, "no current activity"); - } - - } catch ( - Exception err) { - Log.e(NAME, "sendTokenToIntercom error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SEND_TOKEN_TO_INTERCOM, err.toString()); } - } - - @ReactMethod - public void loginUnidentifiedUser(Promise promise) { - Intercom.client().loginUnidentifiedUser(new IntercomStatusCallback() { - @Override - public void onSuccess() { - promise.resolve(true); - } - @Override - public void onFailure(@NonNull IntercomError intercomError) { - Log.e("ERROR", intercomError.getErrorMessage()); - promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); + @ReactMethod + public void handlePushMessage(Promise promise) { + try { + Intercom.client().handlePushMessage(); + promise.resolve(true); + Log.d(NAME, "handlePushMessage"); + } catch (Exception err) { + Log.e(NAME, "handlePushMessage error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.HANDLE_PUSH_MESSAGE, err.toString()); } - }); - } - - @ReactMethod - public void loginUserWithUserAttributes(ReadableMap params, Promise promise) { - Boolean hasEmail = params.hasKey("email") && IntercomHelpers.getValueAsStringForKey(params, "email").length() > 0; - Boolean hasUserId = params.hasKey("userId") && IntercomHelpers.getValueAsStringForKey(params, "userId").length() > 0; - Registration registration = null; - if (hasEmail && hasUserId) { - String email = IntercomHelpers.getValueAsStringForKey(params, "email"); - String userId = IntercomHelpers.getValueAsStringForKey(params, "userId"); - registration = new Registration().withEmail(email).withUserId(userId); - } else if (hasEmail) { - String email = IntercomHelpers.getValueAsStringForKey(params, "email"); - registration = new Registration().withEmail(email); - } else if (hasUserId) { - String userId = IntercomHelpers.getValueAsStringForKey(params, "userId"); - registration = new Registration().withUserId(userId); - } else { - Log.e(NAME, "loginUserWithUserAttributes called with invalid userId or email"); - Log.e(NAME, "You must provide userId or email"); - promise.reject(IntercomErrorCodes.IDENTIFIED_REGISTRATION, "Invalid userId or email"); } - if (registration != null) { - Intercom.client().loginIdentifiedUser(registration, new IntercomStatusCallback() { - @Override - public void onSuccess() { - promise.resolve(true); - } - @Override - public void onFailure(@NonNull IntercomError intercomError) { - Log.e("ERROR", intercomError.getErrorMessage()); - promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); + @ReactMethod + public void sendTokenToIntercom(@NonNull String token, Promise promise) { + try { + Activity activity = getCurrentActivity(); + if (activity != null) { + intercomPushClient.sendTokenToIntercom(activity.getApplication(), token); + Log.d(NAME, "sendTokenToIntercom"); + promise.resolve(true); + } else { + Log.e(NAME, "sendTokenToIntercom"); + Log.e(NAME, "no current activity"); + } + + } catch ( + Exception err) { + Log.e(NAME, "sendTokenToIntercom error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SEND_TOKEN_TO_INTERCOM, err.toString()); } - }); } - } - - @ReactMethod - public void setUserHash(String userHash, Promise promise) { - try { - Intercom.client().setUserHash(userHash); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setUserHash error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_USER_HASH, err.toString()); + + @ReactMethod + public void loginUnidentifiedUser(Promise promise) { + Intercom.client().loginUnidentifiedUser(new IntercomStatusCallback() { + @Override + public void onSuccess() { + promise.resolve(true); + } + + @Override + public void onFailure(@NonNull IntercomError intercomError) { + Log.e("ERROR", intercomError.getErrorMessage()); + promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); + } + }); } - } - - @ReactMethod - public void updateUser(ReadableMap params, Promise promise) { - UserAttributes userAttributes = IntercomHelpers.buildUserAttributes(params); - Intercom.client().updateUser(userAttributes, new IntercomStatusCallback() { - @Override - public void onSuccess() { - promise.resolve(true); - } - @Override - public void onFailure(@NonNull IntercomError intercomError) { - Log.e("ERROR", intercomError.getErrorMessage()); - promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); + @ReactMethod + public void loginUserWithUserAttributes(ReadableMap params, Promise promise) { + Boolean hasEmail = params.hasKey("email") && IntercomHelpers.getValueAsStringForKey(params, "email").length() > 0; + Boolean hasUserId = params.hasKey("userId") && IntercomHelpers.getValueAsStringForKey(params, "userId").length() > 0; + Registration registration = null; + if (hasEmail && hasUserId) { + String email = IntercomHelpers.getValueAsStringForKey(params, "email"); + String userId = IntercomHelpers.getValueAsStringForKey(params, "userId"); + registration = new Registration().withEmail(email).withUserId(userId); + } else if (hasEmail) { + String email = IntercomHelpers.getValueAsStringForKey(params, "email"); + registration = new Registration().withEmail(email); + } else if (hasUserId) { + String userId = IntercomHelpers.getValueAsStringForKey(params, "userId"); + registration = new Registration().withUserId(userId); + } else { + Log.e(NAME, "loginUserWithUserAttributes called with invalid userId or email"); + Log.e(NAME, "You must provide userId or email"); + promise.reject(IntercomErrorCodes.IDENTIFIED_REGISTRATION, "Invalid userId or email"); + } + if (registration != null) { + Intercom.client().loginIdentifiedUser(registration, new IntercomStatusCallback() { + @Override + public void onSuccess() { + promise.resolve(true); + } + + @Override + public void onFailure(@NonNull IntercomError intercomError) { + Log.e("ERROR", intercomError.getErrorMessage()); + promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); + } + }); } - }); - } - - @ReactMethod - public void isUserLoggedIn(Promise promise) { - promise.resolve(Intercom.client().isUserLoggedIn()); - } - - @ReactMethod - public void fetchLoggedInUserAttributes(Promise promise) { - Registration registration = Intercom.client().fetchLoggedInUserAttributes(); - promise.resolve(IntercomHelpers.deconstructRegistration(registration)); - } - - @ReactMethod - public void logout(Promise promise) { - try { - Intercom.client().logout(); - Log.d(NAME, "logout"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "logout error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.LOGOUT, err.toString()); } - } - - @ReactMethod - public void getUnreadConversationCount(Promise promise) { - try { - promise.resolve(Intercom.client().getUnreadConversationCount()); - Log.d(NAME, "getUnreadConversationCount"); - } catch (Exception err) { - Log.e(NAME, "getUnreadConversationCount error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.GET_UNREAD_CONVERSATION, err.toString()); + + @ReactMethod + public void setUserHash(String userHash, Promise promise) { + try { + Intercom.client().setUserHash(userHash); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setUserHash error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_USER_HASH, err.toString()); + } } - } - - @ReactMethod - public void setLogLevel(String logLevel, Promise promise) { - try { - Intercom.setLogLevel(IntercomHelpers.stringToLogLevel(logLevel)); - Log.d(NAME, "setLogLevel"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setLogLevel error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_LOG_LEVEL, err.toString()); + + @ReactMethod + public void updateUser(ReadableMap params, Promise promise) { + UserAttributes userAttributes = IntercomHelpers.buildUserAttributes(params); + Intercom.client().updateUser(userAttributes, new IntercomStatusCallback() { + @Override + public void onSuccess() { + promise.resolve(true); + } + + @Override + public void onFailure(@NonNull IntercomError intercomError) { + Log.e("ERROR", intercomError.getErrorMessage()); + promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); + } + }); } - } - - @ReactMethod - public void logEvent(String eventName, @Nullable ReadableMap metaData, Promise promise) { - try { - if (metaData != null) { - Intercom.client().logEvent(eventName, IntercomHelpers.deconstructReadableMap(metaData, false)); - } else { - Intercom.client().logEvent(eventName); - } - Log.d(NAME, "logEvent"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "logEvent error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.LOG_EVENT_HASH, err.toString()); + + @ReactMethod + public void isUserLoggedIn(Promise promise) { + promise.resolve(Intercom.client().isUserLoggedIn()); } - } - - @ReactMethod - public void presentIntercom(Promise promise) { - try { - Intercom.client().present(); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "presentMessenger error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER, err.toString()); + + @ReactMethod + public void fetchLoggedInUserAttributes(Promise promise) { + Registration registration = Intercom.client().fetchLoggedInUserAttributes(); + promise.resolve(IntercomHelpers.deconstructRegistration(registration)); } - } - - @ReactMethod - public void presentIntercomSpace(String space, Promise promise) { - try { - IntercomSpace selectedSpace = IntercomSpace.Home; - switch (space) { - case "TICKETS": - selectedSpace = IntercomSpace.Tickets; - break; - case "MESSAGES": - selectedSpace = IntercomSpace.Messages; - break; - case "HELP_CENTER": - selectedSpace = IntercomSpace.HelpCenter; - break; - default: - selectedSpace = IntercomSpace.Home; - } - Intercom.client().present(selectedSpace); - promise.resolve(true); - } catch (Exception error) { - Log.e(NAME, "presentIntercomSpace error:"); - Log.e(NAME, error.toString()); - promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER, error.toString()); + + @ReactMethod + public void logout(Promise promise) { + try { + Intercom.client().logout(); + Log.d(NAME, "logout"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "logout error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.LOGOUT, err.toString()); + } } - } - - @ReactMethod - public void presentMessageComposer(@Nullable String initialMessage, Promise promise) { - try { - if (initialMessage != null) { - Intercom.client().displayMessageComposer(initialMessage); - } else { - Intercom.client().displayMessageComposer(); - } - Log.d(NAME, "presentMessageComposer"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "presentMessageComposer error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER_COMPOSER, err.toString()); + + @ReactMethod + public void getUnreadConversationCount(Promise promise) { + try { + promise.resolve(Intercom.client().getUnreadConversationCount()); + Log.d(NAME, "getUnreadConversationCount"); + } catch (Exception err) { + Log.e(NAME, "getUnreadConversationCount error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.GET_UNREAD_CONVERSATION, err.toString()); + } } - } - - @ReactMethod - public void presentContent(ReadableMap params, Promise promise) { - try { - Boolean hasContentType = params.hasKey("type") && params.getString("type").length() > 0; - if (hasContentType) { - IntercomContent content = null; - String contentType = params.getString("type"); - - switch (contentType) { - case "ARTICLE": - content = new IntercomContent.Article(params.getString("id")); - break; - case "CAROUSEL": - content = new IntercomContent.Carousel(params.getString("id")); - break; - case "SURVEY": - content = new IntercomContent.Survey(params.getString("id")); - break; - case "HELP_CENTER_COLLECTIONS": - List collectionIds = IntercomHelpers.readableArrayToStringList(params.getArray("ids")); - content = new IntercomContent.HelpCenterCollections(collectionIds); - break; - case "CONVERSATION": - content = new IntercomContent.Conversation(params.getString("id")); - break; + + @ReactMethod + public void setLogLevel(String logLevel, Promise promise) { + try { + Intercom.setLogLevel(IntercomHelpers.stringToLogLevel(logLevel)); + Log.d(NAME, "setLogLevel"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setLogLevel error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_LOG_LEVEL, err.toString()); } - if (content != null) { - Intercom.client().presentContent(content); - promise.resolve(true); - } else { - promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, "Invalid content type"); + } + + @ReactMethod + public void logEvent(String eventName, @Nullable ReadableMap metaData, Promise promise) { + try { + if (metaData != null) { + Intercom.client().logEvent(eventName, IntercomHelpers.deconstructReadableMap(metaData, false)); + } else { + Intercom.client().logEvent(eventName); + } + Log.d(NAME, "logEvent"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "logEvent error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.LOG_EVENT_HASH, err.toString()); } - } else { - promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, "Intercom content must have a type"); - } - } catch (Exception error) { - Log.e(NAME, error.toString()); - promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, error.toString()); } - } + @ReactMethod + public void presentIntercom(Promise promise) { + try { + Intercom.client().present(); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "presentMessenger error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER, err.toString()); + } + } - @ReactMethod - public void fetchHelpCenterCollections(Promise promise) { - try { + @ReactMethod + public void presentIntercomSpace(String space, Promise promise) { + try { + IntercomSpace selectedSpace = IntercomSpace.Home; + switch (space) { + case "TICKETS": + selectedSpace = IntercomSpace.Tickets; + break; + case "MESSAGES": + selectedSpace = IntercomSpace.Messages; + break; + case "HELP_CENTER": + selectedSpace = IntercomSpace.HelpCenter; + break; + default: + selectedSpace = IntercomSpace.Home; + } + Intercom.client().present(selectedSpace); + promise.resolve(true); + } catch (Exception error) { + Log.e(NAME, "presentIntercomSpace error:"); + Log.e(NAME, error.toString()); + promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER, error.toString()); + } + } - CollectionRequestCallback collectionRequestCallback = new CollectionRequestCallback() { - @Override - public void onComplete(@NotNull List list) { - promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsToReadableArray(list)); + @ReactMethod + public void presentMessageComposer(@Nullable String initialMessage, Promise promise) { + try { + if (initialMessage != null) { + Intercom.client().displayMessageComposer(initialMessage); + } else { + Intercom.client().displayMessageComposer(); + } + Log.d(NAME, "presentMessageComposer"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "presentMessageComposer error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER_COMPOSER, err.toString()); } + } - @Override - public void onError(int i) { - Log.e(NAME, "fetchHelpCenterCollections error"); - promise.reject(String.valueOf(i), "fetchHelpCenterCollections error"); + @ReactMethod + public void presentContent(ReadableMap params, Promise promise) { + try { + Boolean hasContentType = params.hasKey("type") && params.getString("type").length() > 0; + if (hasContentType) { + IntercomContent content = null; + String contentType = params.getString("type"); + + switch (contentType) { + case "ARTICLE": + content = new IntercomContent.Article(params.getString("id")); + break; + case "CAROUSEL": + content = new IntercomContent.Carousel(params.getString("id")); + break; + case "SURVEY": + content = new IntercomContent.Survey(params.getString("id")); + break; + case "HELP_CENTER_COLLECTIONS": + List collectionIds = IntercomHelpers.readableArrayToStringList(params.getArray("ids")); + content = new IntercomContent.HelpCenterCollections(collectionIds); + break; + case "CONVERSATION": + content = new IntercomContent.Conversation(params.getString("id")); + break; + } + if (content != null) { + Intercom.client().presentContent(content); + promise.resolve(true); + } else { + promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, "Invalid content type"); + } + } else { + promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, "Intercom content must have a type"); + } + } catch (Exception error) { + Log.e(NAME, error.toString()); + promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, error.toString()); + } + } + + + @ReactMethod + public void fetchHelpCenterCollections(Promise promise) { + try { + + CollectionRequestCallback collectionRequestCallback = new CollectionRequestCallback() { + @Override + public void onComplete(@NotNull List list) { + promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsToReadableArray(list)); + } + + @Override + public void onError(int i) { + Log.e(NAME, "fetchHelpCenterCollections error"); + promise.reject(String.valueOf(i), "fetchHelpCenterCollections error"); + } + + @Override + public void onFailure() { + Log.e(NAME, "fetchHelpCenterCollections failure"); + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, "fetchHelpCenterCollections failure"); + } + }; + Log.d(NAME, "fetchHelpCenterCollections"); + Intercom.client().fetchHelpCenterCollections(collectionRequestCallback); + + } catch (Exception err) { + Log.e(NAME, "fetchHelpCenterCollections error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, err.toString()); } + } - @Override - public void onFailure() { - Log.e(NAME, "fetchHelpCenterCollections failure"); - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, "fetchHelpCenterCollections failure"); + @ReactMethod + public void fetchHelpCenterCollection(String collectionId, Promise promise) { + try { + if (collectionId.equals("")) { + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, "collectionID can\'t be empty"); + } else { + CollectionContentRequestCallback collectionContentCallback = new CollectionContentRequestCallback() { + @Override + public void onComplete(@NotNull HelpCenterCollectionContent helpCenterCollectionContent) { + promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsContentToReadableMap(helpCenterCollectionContent)); + } + + @Override + public void onError(int i) { + Log.e(NAME, "fetchHelpCenterCollection error"); + promise.reject(String.valueOf(i), "fetchHelpCenterCollection error"); + } + + @Override + public void onFailure() { + Log.e(NAME, "fetchHelpCenterCollection failure"); + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, "fetchHelpCenterCollection failure"); + } + }; + Log.d(NAME, "fetchHelpCenterCollection"); + Intercom.client().fetchHelpCenterCollection(collectionId, collectionContentCallback); + } + + } catch (Exception err) { + Log.e(NAME, "fetchHelpCenterCollection error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, err.toString()); } - }; - Log.d(NAME, "fetchHelpCenterCollections"); - Intercom.client().fetchHelpCenterCollections(collectionRequestCallback); - - } catch (Exception err) { - Log.e(NAME, "fetchHelpCenterCollections error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, err.toString()); } - } - - @ReactMethod - public void fetchHelpCenterCollection(String collectionId, Promise promise) { - try { - if (collectionId.equals("")) { - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, "collectionID can\'t be empty"); - } else { - CollectionContentRequestCallback collectionContentCallback = new CollectionContentRequestCallback() { - @Override - public void onComplete(@NotNull HelpCenterCollectionContent helpCenterCollectionContent) { - promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsContentToReadableMap(helpCenterCollectionContent)); - } - - @Override - public void onError(int i) { - Log.e(NAME, "fetchHelpCenterCollection error"); - promise.reject(String.valueOf(i), "fetchHelpCenterCollection error"); - } - - @Override - public void onFailure() { - Log.e(NAME, "fetchHelpCenterCollection failure"); - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, "fetchHelpCenterCollection failure"); - } - }; - Log.d(NAME, "fetchHelpCenterCollection"); - Intercom.client().fetchHelpCenterCollection(collectionId, collectionContentCallback); - } - - } catch (Exception err) { - Log.e(NAME, "fetchHelpCenterCollection error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, err.toString()); + + @ReactMethod + public void searchHelpCenter(String searchTerm, Promise promise) { + if (searchTerm.equals("")) { + promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, "searchTerm can\'t be empty"); + } else { + try { + SearchRequestCallback collectionContentCallback = new SearchRequestCallback() { + @Override + public void onComplete(@NotNull List helpCenterArticleSearchResult) { + promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterArticleSearchToReadableArray(helpCenterArticleSearchResult)); + } + + @Override + public void onError(int i) { + Log.e(NAME, "searchHelpCenter error"); + promise.reject(String.valueOf(i), "searchHelpCenter error"); + } + + @Override + public void onFailure() { + Log.e(NAME, "searchHelpCenter failure"); + promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, "searchHelpCenter failure"); + } + }; + Log.d(NAME, "searchHelpCenter"); + Intercom.client().searchHelpCenter(searchTerm, collectionContentCallback); + + } catch (Exception err) { + Log.e(NAME, "searchHelpCenter error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, err.toString()); + } + } } - } - - @ReactMethod - public void searchHelpCenter(String searchTerm, Promise promise) { - if (searchTerm.equals("")) { - promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, "searchTerm can\'t be empty"); - } else { - try { - SearchRequestCallback collectionContentCallback = new SearchRequestCallback() { - @Override - public void onComplete(@NotNull List helpCenterArticleSearchResult) { - promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterArticleSearchToReadableArray(helpCenterArticleSearchResult)); - } - - @Override - public void onError(int i) { - Log.e(NAME, "searchHelpCenter error"); - promise.reject(String.valueOf(i), "searchHelpCenter error"); - } - - @Override - public void onFailure() { - Log.e(NAME, "searchHelpCenter failure"); - promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, "searchHelpCenter failure"); - } - }; - Log.d(NAME, "searchHelpCenter"); - Intercom.client().searchHelpCenter(searchTerm, collectionContentCallback); - - } catch (Exception err) { - Log.e(NAME, "searchHelpCenter error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, err.toString()); - } + + @ReactMethod + public void setInAppMessageVisibility(String visibility, Promise promise) { + try { + Intercom.client().setInAppMessageVisibility(IntercomHelpers.stringToVisibility(visibility)); + Log.d(NAME, "setInAppMessageVisibility"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setInAppMessageVisibility error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_IN_APP_MESSAGE_VISIBILITY, err.toString()); + } } - } - - @ReactMethod - public void setInAppMessageVisibility(String visibility, Promise promise) { - try { - Intercom.client().setInAppMessageVisibility(IntercomHelpers.stringToVisibility(visibility)); - Log.d(NAME, "setInAppMessageVisibility"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setInAppMessageVisibility error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_IN_APP_MESSAGE_VISIBILITY, err.toString()); + + @ReactMethod + public void hideIntercom(Promise promise) { + try { + Intercom.client().hideIntercom(); + Log.d(NAME, "hideIntercom"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "hideIntercom error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.HIDE_INTERCOM, err.toString()); + } } - } - - @ReactMethod - public void hideIntercom(Promise promise) { - try { - Intercom.client().hideIntercom(); - Log.d(NAME, "hideIntercom"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "hideIntercom error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.HIDE_INTERCOM, err.toString()); + + @ReactMethod + public void setLauncherVisibility(String visibility, Promise promise) { + try { + Intercom.client().setLauncherVisibility(IntercomHelpers.stringToVisibility(visibility)); + Log.d(NAME, "setInAppMessageVisibility"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setInAppMessageVisibility error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_LAUNCHER_VISIBILITY, err.toString()); + } } - } - - @ReactMethod - public void setLauncherVisibility(String visibility, Promise promise) { - try { - Intercom.client().setLauncherVisibility(IntercomHelpers.stringToVisibility(visibility)); - Log.d(NAME, "setInAppMessageVisibility"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setInAppMessageVisibility error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_LAUNCHER_VISIBILITY, err.toString()); + + @ReactMethod + public void setBottomPadding(int paddingBottom, Promise promise) { + try { + Intercom.client().setBottomPadding(paddingBottom); + Log.d(NAME, "setBottomPadding"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setBottomPadding error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_BOTTOM_PADDING, err.toString()); + } } - } - - @ReactMethod - public void setBottomPadding(int paddingBottom, Promise promise) { - try { - Intercom.client().setBottomPadding(paddingBottom); - Log.d(NAME, "setBottomPadding"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setBottomPadding error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_BOTTOM_PADDING, err.toString()); + + @ReactMethod + public void setUserJwt(String jwt, Promise promise) { + try { + Intercom.client().setUserJwt(jwt); + Log.d(NAME, "Setting JWT"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "Error Setting JWT:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_USER_JWT, err.toString()); + } } - } - - @ReactMethod - public void setUserJwt(String jwt, Promise promise) { - try { - Intercom.client().setUserJwt(jwt); - Log.d(NAME, "Setting JWT"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "Error Setting JWT:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_USER_JWT, err.toString()); + + @ReactMethod + public void initialize(String apiKey, String appId, Promise promise) { + try { + Activity activity = getCurrentActivity(); + if (activity != null) { + Intercom.initialize(activity.getApplication(), apiKey, appId); + promise.resolve(true); + Log.d(NAME, "Intercom initialized with API key and App ID"); + } else { + promise.reject(IntercomErrorCodes.INITIALIZE_ERROR, "Activity is null"); + } + } catch (Exception err) { + Log.e(NAME, "initialize error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.INITIALIZE_ERROR, err.toString()); + } } - } - public static synchronized void initialize(Application application, String apiKey, String appId) { - String sdkVersion = BuildConfig.INTERCOM_VERSION_NAME; - ReactNativeHeaderInterceptor.setReactNativeVersion(application.getApplicationContext(), sdkVersion); - Intercom.initialize(application, apiKey, appId); - } + public static synchronized void initialize(Application application, String apiKey, String appId) { + String sdkVersion = BuildConfig.INTERCOM_VERSION_NAME; + ReactNativeHeaderInterceptor.setReactNativeVersion(application.getApplicationContext(), sdkVersion); + Intercom.initialize(application, apiKey, appId); + } } diff --git a/ios/IntercomModule.m b/ios/IntercomModule.m index 49adc545..17d6c77b 100644 --- a/ios/IntercomModule.m +++ b/ios/IntercomModule.m @@ -16,6 +16,7 @@ @implementation IntercomModule NSString *LOG_EVENT = @"105"; NSString *UNREAD_CONVERSATION_COUNT = @"107"; NSString *SET_USER_JWT = @"109"; +NSString *INITIALIZE_ERROR = @"110"; NSString *SEND_TOKEN_TO_INTERCOM = @"302"; NSString *FETCH_HELP_CENTER_COLLECTIONS = @"901"; NSString *FETCH_HELP_CENTER_COLLECTION = @"902"; @@ -27,6 +28,20 @@ - (dispatch_queue_t)methodQueue { return dispatch_get_main_queue(); } +RCT_EXPORT_METHOD(initialize:(NSString *)apiKey + withAppId:(NSString *)appId + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) { + @try { + [IntercomModule initialize:apiKey withAppId:appId]; + resolve(@(YES)); + NSLog(@"Intercom initialized with API key and App ID"); + } @catch (NSException *exception) { + NSLog(@"initialize error: %@", exception.reason); + reject(INITIALIZE_ERROR, @"Failed to initialize Intercom", nil); + } +} + + (void)initialize:(nonnull NSString *)apiKey withAppId:(nonnull NSString *)appId { NSString *version = @"0"; diff --git a/src/expo-plugins/@types.ts b/src/expo-plugins/@types.ts index cfdda924..849994c9 100644 --- a/src/expo-plugins/@types.ts +++ b/src/expo-plugins/@types.ts @@ -5,4 +5,5 @@ export type IntercomPluginProps = { androidApiKey: string; appId: string; intercomRegion?: IntercomRegion; + useManualInit?: boolean; }; diff --git a/src/expo-plugins/index.ts b/src/expo-plugins/index.ts index 4678818a..d1cb796b 100644 --- a/src/expo-plugins/index.ts +++ b/src/expo-plugins/index.ts @@ -37,13 +37,16 @@ const mainApplication: ConfigPlugin = (_config, props) => '' ); - stringContents = appendContentsInsideDeclarationBlock( - stringContents, - 'onCreate', - `IntercomModule.initialize(this, "${props.androidApiKey}", "${ - props.appId - }")${config.modResults.language === 'java' ? ';' : ''}\n` - ); + // Only auto-initialize if useManualInit is not true + if (!props.useManualInit) { + stringContents = appendContentsInsideDeclarationBlock( + stringContents, + 'onCreate', + `IntercomModule.initialize(this, "${props.androidApiKey}", "${ + props.appId + }")${config.modResults.language === 'java' ? ';' : ''}\n` + ); + } config.modResults.contents = stringContents; return config; @@ -116,19 +119,22 @@ const appDelegate: ConfigPlugin = (_config, props) => ) .replace(/\s*IntercomModule\.initialize\((.*), withAppId: (.*)\)/g, ''); - stringContents = isSwift - ? insertContentsInsideSwiftFunctionBlock( - stringContents, - 'application(_:didFinishLaunchingWithOptions:)', - `IntercomModule.initialize("${props.iosApiKey}", withAppId: "${props.appId}")`, - { position: 'tailBeforeLastReturn' } - ) - : insertContentsInsideObjcFunctionBlock( - stringContents, - 'application didFinishLaunchingWithOptions:', - `[IntercomModule initialize:@"${props.iosApiKey}" withAppId:@"${props.appId}"];`, - { position: 'tailBeforeLastReturn' } - ); + // Only auto-initialize if useManualInit is not true + if (!props.useManualInit) { + stringContents = isSwift + ? insertContentsInsideSwiftFunctionBlock( + stringContents, + 'application(_:didFinishLaunchingWithOptions:)', + `IntercomModule.initialize("${props.iosApiKey}", withAppId: "${props.appId}")`, + { position: 'tailBeforeLastReturn' } + ) + : insertContentsInsideObjcFunctionBlock( + stringContents, + 'application didFinishLaunchingWithOptions:', + `[IntercomModule initialize:@"${props.iosApiKey}" withAppId:@"${props.appId}"];`, + { position: 'tailBeforeLastReturn' } + ); + } config.modResults.contents = stringContents; return config; diff --git a/src/index.tsx b/src/index.tsx index 9b304c21..0fc3aa55 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -105,6 +105,16 @@ export enum Space { } export type IntercomType = { + /** + * Initialize Intercom SDK with API key and App ID. + * This allows manual initialization from React Native instead of requiring native code setup. + * + * @param apiKey Your Intercom API key + * @param appId Your Intercom App ID + * @return {Promise} A promise that resolves to true if initialization succeeds + */ + initialize: (apiKey: string, appId: string) => Promise; + /** * Login a unidentified user. * This is a user that doesn't have any identifiable information such as a `userId` or `email`. @@ -312,6 +322,7 @@ export type IntercomType = { }; const Intercom: IntercomType = { + initialize: (apiKey, appId) => IntercomModule.initialize(apiKey, appId), loginUnidentifiedUser: () => IntercomModule.loginUnidentifiedUser(), loginUserWithUserAttributes: (userAttributes) => IntercomModule.loginUserWithUserAttributes(userAttributes), From 6a5cf97a44840e2efb1446baf3ae57b44313916f Mon Sep 17 00:00:00 2001 From: Vlad M Date: Mon, 28 Jul 2025 22:12:15 +0100 Subject: [PATCH 2/2] Fix formatting --- .../intercom/reactnative/IntercomModule.java | 935 +++++++++--------- 1 file changed, 467 insertions(+), 468 deletions(-) diff --git a/android/src/main/java/com/intercom/reactnative/IntercomModule.java b/android/src/main/java/com/intercom/reactnative/IntercomModule.java index 91cd872c..252d0530 100644 --- a/android/src/main/java/com/intercom/reactnative/IntercomModule.java +++ b/android/src/main/java/com/intercom/reactnative/IntercomModule.java @@ -40,515 +40,514 @@ @ReactModule(name = IntercomModule.NAME) public class IntercomModule extends ReactContextBaseJavaModule { - - public static final String NAME = "IntercomModule"; - - private static final IntercomPushClient intercomPushClient = new IntercomPushClient(); - - public IntercomModule(ReactApplicationContext reactContext) { - super(reactContext); - } - - @Override - @NonNull - public String getName() { - return NAME; - } - - public static boolean isIntercomPush(RemoteMessage remoteMessage) { - try { - Map message = remoteMessage.getData(); - return intercomPushClient.isIntercomPush(message); - } catch (Exception err) { - Log.e(NAME, "isIntercomPush error:"); - Log.e(NAME, err.toString()); - return false; - } + public static final String NAME = "IntercomModule"; + + private static final IntercomPushClient intercomPushClient = new IntercomPushClient(); + + public IntercomModule(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + @NonNull + public String getName() { + return NAME; + } + + public static boolean isIntercomPush(RemoteMessage remoteMessage) { + try { + Map message = remoteMessage.getData(); + return intercomPushClient.isIntercomPush(message); + } catch (Exception err) { + Log.e(NAME, "isIntercomPush error:"); + Log.e(NAME, err.toString()); + return false; } + } - public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage + public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage remoteMessage) { - try { - Map message = remoteMessage.getData(); - intercomPushClient.handlePush(application, message); - } catch (Exception err) { - Log.e(NAME, "handleRemotePushMessage error:"); - Log.e(NAME, err.toString()); - } + try { + Map message = remoteMessage.getData(); + intercomPushClient.handlePush(application, message); + } catch (Exception err) { + Log.e(NAME, "handleRemotePushMessage error:"); + Log.e(NAME, err.toString()); } - - public static void sendTokenToIntercom(Application application, @NonNull String token) { - intercomPushClient.sendTokenToIntercom(application, token); + } + + public static void sendTokenToIntercom(Application application, @NonNull String token) { + intercomPushClient.sendTokenToIntercom(application, token); + Log.d(NAME, "sendTokenToIntercom"); + } + + @ReactMethod + public void handlePushMessage(Promise promise) { + try { + Intercom.client().handlePushMessage(); + promise.resolve(true); + Log.d(NAME, "handlePushMessage"); + } catch (Exception err) { + Log.e(NAME, "handlePushMessage error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.HANDLE_PUSH_MESSAGE, err.toString()); + } + } + + @ReactMethod + public void sendTokenToIntercom(@NonNull String token, Promise promise) { + try { + Activity activity = getCurrentActivity(); + if (activity != null) { + intercomPushClient.sendTokenToIntercom(activity.getApplication(), token); Log.d(NAME, "sendTokenToIntercom"); + promise.resolve(true); + } else { + Log.e(NAME, "sendTokenToIntercom"); + Log.e(NAME, "no current activity"); + } + + } catch ( + Exception err) { + Log.e(NAME, "sendTokenToIntercom error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SEND_TOKEN_TO_INTERCOM, err.toString()); } + } + + @ReactMethod + public void loginUnidentifiedUser(Promise promise) { + Intercom.client().loginUnidentifiedUser(new IntercomStatusCallback() { + @Override + public void onSuccess() { + promise.resolve(true); + } - @ReactMethod - public void handlePushMessage(Promise promise) { - try { - Intercom.client().handlePushMessage(); - promise.resolve(true); - Log.d(NAME, "handlePushMessage"); - } catch (Exception err) { - Log.e(NAME, "handlePushMessage error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.HANDLE_PUSH_MESSAGE, err.toString()); + @Override + public void onFailure(@NonNull IntercomError intercomError) { + Log.e("ERROR", intercomError.getErrorMessage()); + promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); } + }); + } + + @ReactMethod + public void loginUserWithUserAttributes(ReadableMap params, Promise promise) { + Boolean hasEmail = params.hasKey("email") && IntercomHelpers.getValueAsStringForKey(params, "email").length() > 0; + Boolean hasUserId = params.hasKey("userId") && IntercomHelpers.getValueAsStringForKey(params, "userId").length() > 0; + Registration registration = null; + if (hasEmail && hasUserId) { + String email = IntercomHelpers.getValueAsStringForKey(params, "email"); + String userId = IntercomHelpers.getValueAsStringForKey(params, "userId"); + registration = new Registration().withEmail(email).withUserId(userId); + } else if (hasEmail) { + String email = IntercomHelpers.getValueAsStringForKey(params, "email"); + registration = new Registration().withEmail(email); + } else if (hasUserId) { + String userId = IntercomHelpers.getValueAsStringForKey(params, "userId"); + registration = new Registration().withUserId(userId); + } else { + Log.e(NAME, "loginUserWithUserAttributes called with invalid userId or email"); + Log.e(NAME, "You must provide userId or email"); + promise.reject(IntercomErrorCodes.IDENTIFIED_REGISTRATION, "Invalid userId or email"); } + if (registration != null) { + Intercom.client().loginIdentifiedUser(registration, new IntercomStatusCallback() { + @Override + public void onSuccess() { + promise.resolve(true); + } - @ReactMethod - public void sendTokenToIntercom(@NonNull String token, Promise promise) { - try { - Activity activity = getCurrentActivity(); - if (activity != null) { - intercomPushClient.sendTokenToIntercom(activity.getApplication(), token); - Log.d(NAME, "sendTokenToIntercom"); - promise.resolve(true); - } else { - Log.e(NAME, "sendTokenToIntercom"); - Log.e(NAME, "no current activity"); - } - - } catch ( - Exception err) { - Log.e(NAME, "sendTokenToIntercom error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SEND_TOKEN_TO_INTERCOM, err.toString()); + @Override + public void onFailure(@NonNull IntercomError intercomError) { + Log.e("ERROR", intercomError.getErrorMessage()); + promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); } + }); } - - @ReactMethod - public void loginUnidentifiedUser(Promise promise) { - Intercom.client().loginUnidentifiedUser(new IntercomStatusCallback() { - @Override - public void onSuccess() { - promise.resolve(true); - } - - @Override - public void onFailure(@NonNull IntercomError intercomError) { - Log.e("ERROR", intercomError.getErrorMessage()); - promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); - } - }); + } + + @ReactMethod + public void setUserHash(String userHash, Promise promise) { + try { + Intercom.client().setUserHash(userHash); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setUserHash error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_USER_HASH, err.toString()); } - - @ReactMethod - public void loginUserWithUserAttributes(ReadableMap params, Promise promise) { - Boolean hasEmail = params.hasKey("email") && IntercomHelpers.getValueAsStringForKey(params, "email").length() > 0; - Boolean hasUserId = params.hasKey("userId") && IntercomHelpers.getValueAsStringForKey(params, "userId").length() > 0; - Registration registration = null; - if (hasEmail && hasUserId) { - String email = IntercomHelpers.getValueAsStringForKey(params, "email"); - String userId = IntercomHelpers.getValueAsStringForKey(params, "userId"); - registration = new Registration().withEmail(email).withUserId(userId); - } else if (hasEmail) { - String email = IntercomHelpers.getValueAsStringForKey(params, "email"); - registration = new Registration().withEmail(email); - } else if (hasUserId) { - String userId = IntercomHelpers.getValueAsStringForKey(params, "userId"); - registration = new Registration().withUserId(userId); - } else { - Log.e(NAME, "loginUserWithUserAttributes called with invalid userId or email"); - Log.e(NAME, "You must provide userId or email"); - promise.reject(IntercomErrorCodes.IDENTIFIED_REGISTRATION, "Invalid userId or email"); + } + + @ReactMethod + public void updateUser(ReadableMap params, Promise promise) { + UserAttributes userAttributes = IntercomHelpers.buildUserAttributes(params); + Intercom.client().updateUser(userAttributes, new IntercomStatusCallback() { + @Override + public void onSuccess() { + promise.resolve(true); } - if (registration != null) { - Intercom.client().loginIdentifiedUser(registration, new IntercomStatusCallback() { - @Override - public void onSuccess() { - promise.resolve(true); - } - - @Override - public void onFailure(@NonNull IntercomError intercomError) { - Log.e("ERROR", intercomError.getErrorMessage()); - promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); - } - }); - } - } - @ReactMethod - public void setUserHash(String userHash, Promise promise) { - try { - Intercom.client().setUserHash(userHash); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setUserHash error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_USER_HASH, err.toString()); + @Override + public void onFailure(@NonNull IntercomError intercomError) { + Log.e("ERROR", intercomError.getErrorMessage()); + promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); } + }); + } + + @ReactMethod + public void isUserLoggedIn(Promise promise) { + promise.resolve(Intercom.client().isUserLoggedIn()); + } + + @ReactMethod + public void fetchLoggedInUserAttributes(Promise promise) { + Registration registration = Intercom.client().fetchLoggedInUserAttributes(); + promise.resolve(IntercomHelpers.deconstructRegistration(registration)); + } + + @ReactMethod + public void logout(Promise promise) { + try { + Intercom.client().logout(); + Log.d(NAME, "logout"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "logout error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.LOGOUT, err.toString()); } - - @ReactMethod - public void updateUser(ReadableMap params, Promise promise) { - UserAttributes userAttributes = IntercomHelpers.buildUserAttributes(params); - Intercom.client().updateUser(userAttributes, new IntercomStatusCallback() { - @Override - public void onSuccess() { - promise.resolve(true); - } - - @Override - public void onFailure(@NonNull IntercomError intercomError) { - Log.e("ERROR", intercomError.getErrorMessage()); - promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage()); - } - }); + } + + @ReactMethod + public void getUnreadConversationCount(Promise promise) { + try { + promise.resolve(Intercom.client().getUnreadConversationCount()); + Log.d(NAME, "getUnreadConversationCount"); + } catch (Exception err) { + Log.e(NAME, "getUnreadConversationCount error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.GET_UNREAD_CONVERSATION, err.toString()); } - - @ReactMethod - public void isUserLoggedIn(Promise promise) { - promise.resolve(Intercom.client().isUserLoggedIn()); + } + + @ReactMethod + public void setLogLevel(String logLevel, Promise promise) { + try { + Intercom.setLogLevel(IntercomHelpers.stringToLogLevel(logLevel)); + Log.d(NAME, "setLogLevel"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setLogLevel error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_LOG_LEVEL, err.toString()); } - - @ReactMethod - public void fetchLoggedInUserAttributes(Promise promise) { - Registration registration = Intercom.client().fetchLoggedInUserAttributes(); - promise.resolve(IntercomHelpers.deconstructRegistration(registration)); + } + + @ReactMethod + public void logEvent(String eventName, @Nullable ReadableMap metaData, Promise promise) { + try { + if (metaData != null) { + Intercom.client().logEvent(eventName, IntercomHelpers.deconstructReadableMap(metaData, false)); + } else { + Intercom.client().logEvent(eventName); + } + Log.d(NAME, "logEvent"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "logEvent error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.LOG_EVENT_HASH, err.toString()); } - - @ReactMethod - public void logout(Promise promise) { - try { - Intercom.client().logout(); - Log.d(NAME, "logout"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "logout error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.LOGOUT, err.toString()); - } + } + + @ReactMethod + public void presentIntercom(Promise promise) { + try { + Intercom.client().present(); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "presentMessenger error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER, err.toString()); } - - @ReactMethod - public void getUnreadConversationCount(Promise promise) { - try { - promise.resolve(Intercom.client().getUnreadConversationCount()); - Log.d(NAME, "getUnreadConversationCount"); - } catch (Exception err) { - Log.e(NAME, "getUnreadConversationCount error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.GET_UNREAD_CONVERSATION, err.toString()); - } + } + + @ReactMethod + public void presentIntercomSpace(String space, Promise promise) { + try { + IntercomSpace selectedSpace = IntercomSpace.Home; + switch (space) { + case "TICKETS": + selectedSpace = IntercomSpace.Tickets; + break; + case "MESSAGES": + selectedSpace = IntercomSpace.Messages; + break; + case "HELP_CENTER": + selectedSpace = IntercomSpace.HelpCenter; + break; + default: + selectedSpace = IntercomSpace.Home; + } + Intercom.client().present(selectedSpace); + promise.resolve(true); + } catch (Exception error) { + Log.e(NAME, "presentIntercomSpace error:"); + Log.e(NAME, error.toString()); + promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER, error.toString()); } - - @ReactMethod - public void setLogLevel(String logLevel, Promise promise) { - try { - Intercom.setLogLevel(IntercomHelpers.stringToLogLevel(logLevel)); - Log.d(NAME, "setLogLevel"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setLogLevel error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_LOG_LEVEL, err.toString()); - } + } + + @ReactMethod + public void presentMessageComposer(@Nullable String initialMessage, Promise promise) { + try { + if (initialMessage != null) { + Intercom.client().displayMessageComposer(initialMessage); + } else { + Intercom.client().displayMessageComposer(); + } + Log.d(NAME, "presentMessageComposer"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "presentMessageComposer error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER_COMPOSER, err.toString()); } - - @ReactMethod - public void logEvent(String eventName, @Nullable ReadableMap metaData, Promise promise) { - try { - if (metaData != null) { - Intercom.client().logEvent(eventName, IntercomHelpers.deconstructReadableMap(metaData, false)); - } else { - Intercom.client().logEvent(eventName); - } - Log.d(NAME, "logEvent"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "logEvent error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.LOG_EVENT_HASH, err.toString()); + } + + @ReactMethod + public void presentContent(ReadableMap params, Promise promise) { + try { + Boolean hasContentType = params.hasKey("type") && params.getString("type").length() > 0; + if (hasContentType) { + IntercomContent content = null; + String contentType = params.getString("type"); + + switch (contentType) { + case "ARTICLE": + content = new IntercomContent.Article(params.getString("id")); + break; + case "CAROUSEL": + content = new IntercomContent.Carousel(params.getString("id")); + break; + case "SURVEY": + content = new IntercomContent.Survey(params.getString("id")); + break; + case "HELP_CENTER_COLLECTIONS": + List collectionIds = IntercomHelpers.readableArrayToStringList(params.getArray("ids")); + content = new IntercomContent.HelpCenterCollections(collectionIds); + break; + case "CONVERSATION": + content = new IntercomContent.Conversation(params.getString("id")); + break; } - } - - @ReactMethod - public void presentIntercom(Promise promise) { - try { - Intercom.client().present(); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "presentMessenger error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER, err.toString()); + if (content != null) { + Intercom.client().presentContent(content); + promise.resolve(true); + } else { + promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, "Invalid content type"); } + } else { + promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, "Intercom content must have a type"); + } + } catch (Exception error) { + Log.e(NAME, error.toString()); + promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, error.toString()); } + } - @ReactMethod - public void presentIntercomSpace(String space, Promise promise) { - try { - IntercomSpace selectedSpace = IntercomSpace.Home; - switch (space) { - case "TICKETS": - selectedSpace = IntercomSpace.Tickets; - break; - case "MESSAGES": - selectedSpace = IntercomSpace.Messages; - break; - case "HELP_CENTER": - selectedSpace = IntercomSpace.HelpCenter; - break; - default: - selectedSpace = IntercomSpace.Home; - } - Intercom.client().present(selectedSpace); - promise.resolve(true); - } catch (Exception error) { - Log.e(NAME, "presentIntercomSpace error:"); - Log.e(NAME, error.toString()); - promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER, error.toString()); - } - } - @ReactMethod - public void presentMessageComposer(@Nullable String initialMessage, Promise promise) { - try { - if (initialMessage != null) { - Intercom.client().displayMessageComposer(initialMessage); - } else { - Intercom.client().displayMessageComposer(); - } - Log.d(NAME, "presentMessageComposer"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "presentMessageComposer error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.DISPLAY_MESSENGER_COMPOSER, err.toString()); - } - } + @ReactMethod + public void fetchHelpCenterCollections(Promise promise) { + try { - @ReactMethod - public void presentContent(ReadableMap params, Promise promise) { - try { - Boolean hasContentType = params.hasKey("type") && params.getString("type").length() > 0; - if (hasContentType) { - IntercomContent content = null; - String contentType = params.getString("type"); - - switch (contentType) { - case "ARTICLE": - content = new IntercomContent.Article(params.getString("id")); - break; - case "CAROUSEL": - content = new IntercomContent.Carousel(params.getString("id")); - break; - case "SURVEY": - content = new IntercomContent.Survey(params.getString("id")); - break; - case "HELP_CENTER_COLLECTIONS": - List collectionIds = IntercomHelpers.readableArrayToStringList(params.getArray("ids")); - content = new IntercomContent.HelpCenterCollections(collectionIds); - break; - case "CONVERSATION": - content = new IntercomContent.Conversation(params.getString("id")); - break; - } - if (content != null) { - Intercom.client().presentContent(content); - promise.resolve(true); - } else { - promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, "Invalid content type"); - } - } else { - promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, "Intercom content must have a type"); - } - } catch (Exception error) { - Log.e(NAME, error.toString()); - promise.reject(IntercomErrorCodes.DISPLAY_CONTENT, error.toString()); + CollectionRequestCallback collectionRequestCallback = new CollectionRequestCallback() { + @Override + public void onComplete(@NotNull List list) { + promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsToReadableArray(list)); } - } - - - @ReactMethod - public void fetchHelpCenterCollections(Promise promise) { - try { - - CollectionRequestCallback collectionRequestCallback = new CollectionRequestCallback() { - @Override - public void onComplete(@NotNull List list) { - promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsToReadableArray(list)); - } - - @Override - public void onError(int i) { - Log.e(NAME, "fetchHelpCenterCollections error"); - promise.reject(String.valueOf(i), "fetchHelpCenterCollections error"); - } - - @Override - public void onFailure() { - Log.e(NAME, "fetchHelpCenterCollections failure"); - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, "fetchHelpCenterCollections failure"); - } - }; - Log.d(NAME, "fetchHelpCenterCollections"); - Intercom.client().fetchHelpCenterCollections(collectionRequestCallback); - - } catch (Exception err) { - Log.e(NAME, "fetchHelpCenterCollections error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, err.toString()); - } - } - @ReactMethod - public void fetchHelpCenterCollection(String collectionId, Promise promise) { - try { - if (collectionId.equals("")) { - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, "collectionID can\'t be empty"); - } else { - CollectionContentRequestCallback collectionContentCallback = new CollectionContentRequestCallback() { - @Override - public void onComplete(@NotNull HelpCenterCollectionContent helpCenterCollectionContent) { - promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsContentToReadableMap(helpCenterCollectionContent)); - } - - @Override - public void onError(int i) { - Log.e(NAME, "fetchHelpCenterCollection error"); - promise.reject(String.valueOf(i), "fetchHelpCenterCollection error"); - } - - @Override - public void onFailure() { - Log.e(NAME, "fetchHelpCenterCollection failure"); - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, "fetchHelpCenterCollection failure"); - } - }; - Log.d(NAME, "fetchHelpCenterCollection"); - Intercom.client().fetchHelpCenterCollection(collectionId, collectionContentCallback); - } - - } catch (Exception err) { - Log.e(NAME, "fetchHelpCenterCollection error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, err.toString()); + @Override + public void onError(int i) { + Log.e(NAME, "fetchHelpCenterCollections error"); + promise.reject(String.valueOf(i), "fetchHelpCenterCollections error"); } - } - @ReactMethod - public void searchHelpCenter(String searchTerm, Promise promise) { - if (searchTerm.equals("")) { - promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, "searchTerm can\'t be empty"); - } else { - try { - SearchRequestCallback collectionContentCallback = new SearchRequestCallback() { - @Override - public void onComplete(@NotNull List helpCenterArticleSearchResult) { - promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterArticleSearchToReadableArray(helpCenterArticleSearchResult)); - } - - @Override - public void onError(int i) { - Log.e(NAME, "searchHelpCenter error"); - promise.reject(String.valueOf(i), "searchHelpCenter error"); - } - - @Override - public void onFailure() { - Log.e(NAME, "searchHelpCenter failure"); - promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, "searchHelpCenter failure"); - } - }; - Log.d(NAME, "searchHelpCenter"); - Intercom.client().searchHelpCenter(searchTerm, collectionContentCallback); - - } catch (Exception err) { - Log.e(NAME, "searchHelpCenter error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, err.toString()); - } + @Override + public void onFailure() { + Log.e(NAME, "fetchHelpCenterCollections failure"); + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, "fetchHelpCenterCollections failure"); } + }; + Log.d(NAME, "fetchHelpCenterCollections"); + Intercom.client().fetchHelpCenterCollections(collectionRequestCallback); + + } catch (Exception err) { + Log.e(NAME, "fetchHelpCenterCollections error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTIONS, err.toString()); } - - @ReactMethod - public void setInAppMessageVisibility(String visibility, Promise promise) { - try { - Intercom.client().setInAppMessageVisibility(IntercomHelpers.stringToVisibility(visibility)); - Log.d(NAME, "setInAppMessageVisibility"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setInAppMessageVisibility error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_IN_APP_MESSAGE_VISIBILITY, err.toString()); - } + } + + @ReactMethod + public void fetchHelpCenterCollection(String collectionId, Promise promise) { + try { + if (collectionId.equals("")) { + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, "collectionID can\'t be empty"); + } else { + CollectionContentRequestCallback collectionContentCallback = new CollectionContentRequestCallback() { + @Override + public void onComplete(@NotNull HelpCenterCollectionContent helpCenterCollectionContent) { + promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterCollectionsContentToReadableMap(helpCenterCollectionContent)); + } + + @Override + public void onError(int i) { + Log.e(NAME, "fetchHelpCenterCollection error"); + promise.reject(String.valueOf(i), "fetchHelpCenterCollection error"); + } + + @Override + public void onFailure() { + Log.e(NAME, "fetchHelpCenterCollection failure"); + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, "fetchHelpCenterCollection failure"); + } + }; + Log.d(NAME, "fetchHelpCenterCollection"); + Intercom.client().fetchHelpCenterCollection(collectionId, collectionContentCallback); + } + + } catch (Exception err) { + Log.e(NAME, "fetchHelpCenterCollection error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.FETCH_HELP_CENTER_COLLECTION, err.toString()); } - - @ReactMethod - public void hideIntercom(Promise promise) { - try { - Intercom.client().hideIntercom(); - Log.d(NAME, "hideIntercom"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "hideIntercom error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.HIDE_INTERCOM, err.toString()); - } + } + + @ReactMethod + public void searchHelpCenter(String searchTerm, Promise promise) { + if (searchTerm.equals("")) { + promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, "searchTerm can\'t be empty"); + } else { + try { + SearchRequestCallback collectionContentCallback = new SearchRequestCallback() { + @Override + public void onComplete(@NotNull List helpCenterArticleSearchResult) { + promise.resolve(IntercomHelpCenterHelpers.parseHelpCenterArticleSearchToReadableArray(helpCenterArticleSearchResult)); + } + + @Override + public void onError(int i) { + Log.e(NAME, "searchHelpCenter error"); + promise.reject(String.valueOf(i), "searchHelpCenter error"); + } + + @Override + public void onFailure() { + Log.e(NAME, "searchHelpCenter failure"); + promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, "searchHelpCenter failure"); + } + }; + Log.d(NAME, "searchHelpCenter"); + Intercom.client().searchHelpCenter(searchTerm, collectionContentCallback); + + } catch (Exception err) { + Log.e(NAME, "searchHelpCenter error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SEARCH_HELP_CENTER, err.toString()); + } } - - @ReactMethod - public void setLauncherVisibility(String visibility, Promise promise) { - try { - Intercom.client().setLauncherVisibility(IntercomHelpers.stringToVisibility(visibility)); - Log.d(NAME, "setInAppMessageVisibility"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setInAppMessageVisibility error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_LAUNCHER_VISIBILITY, err.toString()); - } + } + + @ReactMethod + public void setInAppMessageVisibility(String visibility, Promise promise) { + try { + Intercom.client().setInAppMessageVisibility(IntercomHelpers.stringToVisibility(visibility)); + Log.d(NAME, "setInAppMessageVisibility"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setInAppMessageVisibility error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_IN_APP_MESSAGE_VISIBILITY, err.toString()); } - - @ReactMethod - public void setBottomPadding(int paddingBottom, Promise promise) { - try { - Intercom.client().setBottomPadding(paddingBottom); - Log.d(NAME, "setBottomPadding"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "setBottomPadding error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_BOTTOM_PADDING, err.toString()); - } + } + + @ReactMethod + public void hideIntercom(Promise promise) { + try { + Intercom.client().hideIntercom(); + Log.d(NAME, "hideIntercom"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "hideIntercom error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.HIDE_INTERCOM, err.toString()); } - - @ReactMethod - public void setUserJwt(String jwt, Promise promise) { - try { - Intercom.client().setUserJwt(jwt); - Log.d(NAME, "Setting JWT"); - promise.resolve(true); - } catch (Exception err) { - Log.e(NAME, "Error Setting JWT:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.SET_USER_JWT, err.toString()); - } + } + + @ReactMethod + public void setLauncherVisibility(String visibility, Promise promise) { + try { + Intercom.client().setLauncherVisibility(IntercomHelpers.stringToVisibility(visibility)); + Log.d(NAME, "setInAppMessageVisibility"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setInAppMessageVisibility error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_LAUNCHER_VISIBILITY, err.toString()); } - - @ReactMethod - public void initialize(String apiKey, String appId, Promise promise) { - try { - Activity activity = getCurrentActivity(); - if (activity != null) { - Intercom.initialize(activity.getApplication(), apiKey, appId); - promise.resolve(true); - Log.d(NAME, "Intercom initialized with API key and App ID"); - } else { - promise.reject(IntercomErrorCodes.INITIALIZE_ERROR, "Activity is null"); - } - } catch (Exception err) { - Log.e(NAME, "initialize error:"); - Log.e(NAME, err.toString()); - promise.reject(IntercomErrorCodes.INITIALIZE_ERROR, err.toString()); - } + } + + @ReactMethod + public void setBottomPadding(int paddingBottom, Promise promise) { + try { + Intercom.client().setBottomPadding(paddingBottom); + Log.d(NAME, "setBottomPadding"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "setBottomPadding error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_BOTTOM_PADDING, err.toString()); } - - public static synchronized void initialize(Application application, String apiKey, String appId) { - String sdkVersion = BuildConfig.INTERCOM_VERSION_NAME; - ReactNativeHeaderInterceptor.setReactNativeVersion(application.getApplicationContext(), sdkVersion); - Intercom.initialize(application, apiKey, appId); + } + + @ReactMethod + public void setUserJwt(String jwt, Promise promise) { + try { + Intercom.client().setUserJwt(jwt); + Log.d(NAME, "Setting JWT"); + promise.resolve(true); + } catch (Exception err) { + Log.e(NAME, "Error Setting JWT:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.SET_USER_JWT, err.toString()); + } + } + + @ReactMethod + public void initialize(String apiKey, String appId, Promise promise) { + try { + Activity activity = getCurrentActivity(); + if (activity != null) { + Intercom.initialize(activity.getApplication(), apiKey, appId); + promise.resolve(true); + Log.d(NAME, "Intercom initialized with API key and App ID"); + } else { + promise.reject(IntercomErrorCodes.INITIALIZE_ERROR, "Activity is null"); + } + } catch (Exception err) { + Log.e(NAME, "initialize error:"); + Log.e(NAME, err.toString()); + promise.reject(IntercomErrorCodes.INITIALIZE_ERROR, err.toString()); } + } + + public static synchronized void initialize(Application application, String apiKey, String appId) { + String sdkVersion = BuildConfig.INTERCOM_VERSION_NAME; + ReactNativeHeaderInterceptor.setReactNativeVersion(application.getApplicationContext(), sdkVersion); + Intercom.initialize(application, apiKey, appId); + } }