Skip to content

Commit 7788cb7

Browse files
author
Pierre-Alexandre Dupuy
committed
Fix android visitor info and bump android dependencies
1 parent 718daa9 commit 7788cb7

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ In Xcode, drag and drop `node_modules/react-native-zendesk-chat/RNZendeskChat.m`
120120
// ...
121121

122122
// Inside the appropriate appDidFinishLaunching method
123-
[ZDCChat initializeWithAccountKey:@"YOUR_ZENDESK_ACCOUNT_KEY"];
123+
[ZDCChat initializeWithAccountKey:@"YOUR_ZENDESK_ACCOUNT_KEY" appId:"YOUR_ZENDESK_APP_ID"];
124124

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

168168
// Call this once in your Activity's bootup lifecycle
169-
Chat.INSTANCE.init(mReactContext, key);
169+
Chat.INSTANCE.init(mReactContext, key, appId);
170170
```
171171

172172
## Contributing

android/src/main/java/com/taskrabbit/zendesk/RNZendeskChatModule.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import com.facebook.react.bridge.WritableNativeMap;
1414
import zendesk.chat.Chat;
1515
import zendesk.chat.ChatConfiguration;
16+
import zendesk.chat.ChatSessionStatus;
17+
import zendesk.chat.ChatState;
18+
import zendesk.chat.ObservationScope;
19+
import zendesk.chat.Observer;
1620
import zendesk.chat.ProfileProvider;
1721
import zendesk.chat.PreChatFormFieldStatus;
1822
import zendesk.chat.ChatEngine;
@@ -28,6 +32,10 @@ public class RNZendeskChatModule extends ReactContextBaseJavaModule {
2832

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

35+
private boolean visitorSet = false;
36+
private ProfileProvider profileProvider;
37+
private VisitorInfo visitorInfo;
38+
3139
// private class Converters {
3240
public static ArrayList<String> getArrayListOfStrings(ReadableMap options, String key, String functionHint) {
3341
ArrayList<String> result = new ArrayList();
@@ -157,14 +165,16 @@ public void setVisitorInfo(ReadableMap options) {
157165
builder = builder.withPhoneNumber(phone);
158166
}
159167

160-
VisitorInfo visitorData = builder.build();
168+
visitorInfo = builder.build();
161169

162170
if (Chat.INSTANCE.providers() == null) {
163171
Log.e(TAG,
164172
"Zendesk Internals are undefined -- did you forget to call RNZendeskModule.init(<account_key>)?");
165173
return;
166174
}
167-
Chat.INSTANCE.providers().profileProvider().setVisitorInfo(visitorData, null);
175+
176+
profileProvider = Chat.INSTANCE.providers().profileProvider();
177+
profileProvider.setVisitorInfo(visitorInfo, null);
168178
}
169179

170180
@ReactMethod
@@ -251,6 +261,7 @@ public void startChat(ReadableMap options) {
251261
return;
252262
}
253263
setVisitorInfo(options);
264+
setupObserver();
254265

255266
ReadableMap flagHash = RNZendeskChatModule.getReadableMap(options, "behaviorFlags", "startChat");
256267
boolean showPreChatForm = getBooleanOrDefault(flagHash, "showPreChatForm", "startChat(behaviorFlags)", true);
@@ -288,4 +299,27 @@ public void registerPushToken(String token) {
288299
pushProvider.registerPushToken(token);
289300
}
290301
}
302+
303+
public void setupObserver(){
304+
final ObservationScope observationScope = new ObservationScope();
305+
Chat.INSTANCE.providers().chatProvider().observeChatState(observationScope, new Observer<ChatState>() {
306+
@Override
307+
public void update(ChatState chatState) {
308+
ChatSessionStatus chatStatus = chatState.getChatSessionStatus();
309+
// Status achieved after the PreChatForm is completed
310+
if (chatStatus == ChatSessionStatus.STARTED) {
311+
// Update the information MID chat here. All info but Department can be updated
312+
if (!visitorSet) {
313+
// Add here the code to set the visitor info - visitorInfo would be a VisitorInfo type variable containing all the information to set
314+
profileProvider.setVisitorInfo(visitorInfo, null);
315+
visitorSet = true;
316+
}
317+
318+
} else {
319+
// There are few other statuses that you can observe but they are unused in this example
320+
Log.d(TAG, "[observerSetup] - ChatSessionUpdate -> (unused) status : " + chatStatus.toString());
321+
}
322+
}
323+
});
324+
}
291325
}

0 commit comments

Comments
 (0)