Skip to content

Fix android visitor info #82

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

Merged
merged 1 commit into from
May 5, 2021
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ In Xcode, drag and drop `node_modules/react-native-zendesk-chat/RNZendeskChat.m`
// ...

// Inside the appropriate appDidFinishLaunching method
[ZDCChat initializeWithAccountKey:@"YOUR_ZENDESK_ACCOUNT_KEY"];
[ZDCChat initializeWithAccountKey:@"YOUR_ZENDESK_ACCOUNT_KEY" appId:"YOUR_ZENDESK_APP_ID"];

// And access other interesting APIs
```
Expand Down Expand Up @@ -166,7 +166,7 @@ compile project(':react-native-zendesk-chat')
// Note: there is a JS method to do this -- prefer doing that! -- This is for advanced users only.

// Call this once in your Activity's bootup lifecycle
Chat.INSTANCE.init(mReactContext, key);
Chat.INSTANCE.init(mReactContext, key, appId);
```

## Contributing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import com.facebook.react.bridge.WritableNativeMap;
import zendesk.chat.Chat;
import zendesk.chat.ChatConfiguration;
import zendesk.chat.ChatSessionStatus;
import zendesk.chat.ChatState;
import zendesk.chat.ObservationScope;
import zendesk.chat.Observer;
import zendesk.chat.ProfileProvider;
import zendesk.chat.PreChatFormFieldStatus;
import zendesk.chat.ChatEngine;
Expand All @@ -28,6 +32,10 @@ public class RNZendeskChatModule extends ReactContextBaseJavaModule {

private ArrayList<String> currentUserTags = new ArrayList();

private boolean visitorSet = false;
private ProfileProvider profileProvider;
private VisitorInfo visitorInfo;

// private class Converters {
public static ArrayList<String> getArrayListOfStrings(ReadableMap options, String key, String functionHint) {
ArrayList<String> result = new ArrayList();
Expand Down Expand Up @@ -157,14 +165,16 @@ public void setVisitorInfo(ReadableMap options) {
builder = builder.withPhoneNumber(phone);
}

VisitorInfo visitorData = builder.build();
visitorInfo = builder.build();

if (Chat.INSTANCE.providers() == null) {
Log.e(TAG,
"Zendesk Internals are undefined -- did you forget to call RNZendeskModule.init(<account_key>)?");
return;
}
Chat.INSTANCE.providers().profileProvider().setVisitorInfo(visitorData, null);

profileProvider = Chat.INSTANCE.providers().profileProvider();
profileProvider.setVisitorInfo(visitorInfo, null);
}

@ReactMethod
Expand Down Expand Up @@ -251,6 +261,7 @@ public void startChat(ReadableMap options) {
return;
}
setVisitorInfo(options);
setupObserver();

ReadableMap flagHash = RNZendeskChatModule.getReadableMap(options, "behaviorFlags", "startChat");
boolean showPreChatForm = getBooleanOrDefault(flagHash, "showPreChatForm", "startChat(behaviorFlags)", true);
Expand Down Expand Up @@ -288,4 +299,27 @@ public void registerPushToken(String token) {
pushProvider.registerPushToken(token);
}
}

public void setupObserver(){
final ObservationScope observationScope = new ObservationScope();
Chat.INSTANCE.providers().chatProvider().observeChatState(observationScope, new Observer<ChatState>() {
@Override
public void update(ChatState chatState) {
ChatSessionStatus chatStatus = chatState.getChatSessionStatus();
// Status achieved after the PreChatForm is completed
if (chatStatus == ChatSessionStatus.STARTED) {
// Update the information MID chat here. All info but Department can be updated
if (!visitorSet) {
// Add here the code to set the visitor info - visitorInfo would be a VisitorInfo type variable containing all the information to set
profileProvider.setVisitorInfo(visitorInfo, null);
visitorSet = true;
}

} else {
// There are few other statuses that you can observe but they are unused in this example
Log.d(TAG, "[observerSetup] - ChatSessionUpdate -> (unused) status : " + chatStatus.toString());
}
}
});
}
}