diff --git a/android/src/main/java/com/reactnativezendeskmessaging/ZendeskMessagingModule.java b/android/src/main/java/com/reactnativezendeskmessaging/ZendeskMessagingModule.java index 21c3752..8e3db50 100644 --- a/android/src/main/java/com/reactnativezendeskmessaging/ZendeskMessagingModule.java +++ b/android/src/main/java/com/reactnativezendeskmessaging/ZendeskMessagingModule.java @@ -10,37 +10,82 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.module.annotations.ReactModule; +import org.json.JSONException; +import org.json.JSONObject; + +import kotlin.Unit; +import zendesk.android.FailureCallback; +import zendesk.android.SuccessCallback; import zendesk.android.Zendesk; +import zendesk.android.ZendeskUser; import zendesk.messaging.android.DefaultMessagingFactory; @ReactModule(name = ZendeskMessagingModule.NAME) public class ZendeskMessagingModule extends ReactContextBaseJavaModule { - public static final String NAME = "ZendeskMessaging"; - private final ReactApplicationContext reactContext; - - public ZendeskMessagingModule(ReactApplicationContext reactContext) { - super(reactContext); - this.reactContext = reactContext; - } - - @Override - @NonNull - public String getName() { - return NAME; - } - - @ReactMethod - public void initialize(String channelKey) { - Zendesk.initialize( - this.reactContext, - channelKey, - zendesk -> Log.i("IntegrationApplication", "Initialization successful"), - error -> Log.e("IntegrationApplication", "Messaging failed to initialize", error), - new DefaultMessagingFactory()); - } - - @ReactMethod - public void showMessaging() { - Zendesk.getInstance().getMessaging().showMessaging(this.reactContext.getCurrentActivity()); - } + public static final String NAME = "ZendeskMessaging"; + private final ReactApplicationContext reactContext; + + public ZendeskMessagingModule(ReactApplicationContext reactContext) { + super(reactContext); + this.reactContext = reactContext; + } + + @Override + @NonNull + public String getName() { + return NAME; + } + + @ReactMethod + public void initialize(String channelKey, Promise promise) { + Zendesk.initialize( + this.reactContext, + channelKey, + zendesk -> promise.resolve("success"), + error -> promise.reject(error), + new DefaultMessagingFactory()); + } + + @ReactMethod + public void showMessaging() { + Zendesk.getInstance().getMessaging().showMessaging(this.reactContext.getCurrentActivity()); + } + + @ReactMethod + public void loginUser(String token, Promise promise) { + Zendesk.getInstance().loginUser(token, new SuccessCallback() { + @Override + public void onSuccess(ZendeskUser value) { + JSONObject jsonObject= new JSONObject(); + try { + jsonObject.put("externalId", value.getExternalId()); + promise.resolve(jsonObject.toString()); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + promise.reject(e); + } + } + }, new FailureCallback() { + @Override + public void onFailure(@NonNull Throwable error) { + promise.reject(error); + } + }); + } + + @ReactMethod + public void logoutUser(Promise promise) { + Zendesk.getInstance().logoutUser(new SuccessCallback() { + @Override + public void onSuccess(Unit value) { + promise.resolve("success"); + } + }, new FailureCallback() { + @Override + public void onFailure(@NonNull Throwable error) { + promise.reject(error); + } + }); + } } diff --git a/example/.env b/example/.env index 05440e5..e16f874 100644 --- a/example/.env +++ b/example/.env @@ -1,2 +1,3 @@ CHANNEL_KEY_ANDROID= -CHANNEL_KEY_IOS= \ No newline at end of file +CHANNEL_KEY_IOS= +JWT_TOKEN= \ No newline at end of file diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 3016c94..c2ab447 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -213,7 +213,7 @@ PODS: - react-native-config/App (= 1.4.5) - react-native-config/App (1.4.5): - React-Core - - react-native-zendesk-messaging (0.1.0): + - react-native-zendesk-messaging (1.0.0): - React-Core - ZendeskSDKMessaging - React-perflogger (0.67.2) @@ -438,7 +438,7 @@ SPEC CHECKSUMS: React-jsinspector: 595f76eba2176ebd8817a1fffd47b84fbdab9383 React-logger: 23de8ea0f44fa00ee77e96060273225607fd4d78 react-native-config: 6502b1879f97ed5ac570a029961fc35ea606cd14 - react-native-zendesk-messaging: 3a72c6d58680ed4cbc92ed14ad3b422d4adb5db1 + react-native-zendesk-messaging: e44d8e88ecf7af04d9851ec68840afb8d3147ce3 React-perflogger: 3c9bb7372493e49036f07a82c44c8cf65cbe88db React-RCTActionSheet: 052606483045a408693aa7e864410b4a052f541a React-RCTAnimation: 08d4cac13222bb1348c687a0158dfd3b577cdb63 diff --git a/example/src/App.tsx b/example/src/App.tsx index 5667f63..abbe8e9 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -4,16 +4,49 @@ import Config from 'react-native-config'; import { initialize, showMessaging, + loginUser, + logoutUser, } from '@flashcoffee/react-native-zendesk-messaging'; const App = () => { + const [loadingInit, setLoadingInit] = React.useState(true); + const [isLogin, setIsLogin] = React.useState(false); React.useEffect(() => { - initialize( + const channelKey = Platform.OS === 'android' ? Config.CHANNEL_KEY_ANDROID - : Config.CHANNEL_KEY_IOS + : Config.CHANNEL_KEY_IOS; + initialize( + channelKey, + () => { + setLoadingInit(false); + }, + (err: any) => { + setLoadingInit(false); + console.log(err); + } ); }, []); + + const login = () => { + loginUser( + Config.JWT_TOKEN, + (user) => { + setIsLogin(true); + console.log('isLoading', user); + }, + (err) => { + console.log('error', err); + } + ); + }; + + const logout = () => { + logoutUser(() => { + setIsLogin(false); + }); + }; + return ( { > Zendesk Messaging - - Press The "CHAT" button to test - -