crisp-im / crisp-sdk-android

:package: Crisp Android SDK, add a chat in any Android app and communicate with your users.
https://docs.crisp.chat/guides/chatbox-sdks/android-sdk/
Other
56 stars 17 forks source link

The session is not recovers in the android SDK #188

Open reygie25 opened 2 months ago

reygie25 commented 2 months ago

The scenario here is when the web sdk started a session.

Web SDK

To send or initiate a first-time message to a contact or workers (which we call), using the Web SDK, we open the chatbox to generate a session_id which is needed to send the message. Here's our example code:

{
  // 1. Initialize Crisp
  Crisp.configure(website_id, { autoload: false });
  Crisp.setTokenId(user_id); // Example: 6678c287a61b0b0015a1e816
  Crisp.user.setEmail(email);
  Crisp.user.setNickname(name);

  // 2. Event listener for session loaded
  Crisp.session.onLoaded((sessionId) => {
    console.log("Session Loaded!", sessionId); // We used this sessionId to send message. Example: session_76f9db48-4b51-463e-8a9c-0a56e3854c21
  });
  Crisp.load();

  // 3. Open chatbox to generate session id and close it after 500ms
  Crisp.chat.show();
  Crisp.chat.open();
  setTimeout(() => {
    Crisp.chat.close();
    Crisp.chat.hide();
  }, 500);
}

ANDROID SDK

  1. Open the crisp chat view with token ID 6678c287a61b0b0015a1e816, below is the code we called before the crisp chatview opens.
    Crisp.setTokenID(context, profile.getUserId());
    Crisp.setSessionString("user_id", profile.getUserId());
    String topic = profile.getUserId() + "-android";
    Crisp.setSessionString("topic", topic);
    Crisp.setUserNickname(profile.getFullName());
    Crisp.setUserEmail(profile.getEmail());
    Crisp.setUserAvatar(profile.getProfileImageUrl()); 
    startActivity(new Intent(context, im.crisp.client.ChatActivity.class));
  2. The session will not recover and created a new session: session_6dbd6617-aa65-45eb-858a-7002647bbc43
  3. The Android app is expecting to recover the session ID: session_76f9db48-4b51-463e-8a9c-0a56e3854c21
Doc1faux commented 2 months ago

Hi @reygie25 and thank you for your feedback.

On my side, I use the HTML integration for test purposes, not the npm package, but it should behave the same way as it's only a wrapper. However, the following works well on my side, I recover the same session_id on both Web and Android:

Web

<script type="text/javascript">
  $crisp=[];
  CRISP_WEBSITE_ID=website_id;
  CRISP_TOKEN_ID=user_id;
  (function(){d=document;s=d.createElement("script");s.src="https://client.crisp.chat/l.js";s.async=1;d.getElementsByTagName("head")[0].appendChild(s);})();
</script>

Android

Crisp.setWebsiteId(context, website_id);
Crisp.resetChatSession();
Crisp.setTokenID(context, user_id);
startActivity(new Intent(context, ChatActivity.class));

Be sure to call Crisp.resetChatSession() before setting the TokenID, it will do nothing if a session already exists! See documentation.

reygie25 commented 2 months ago

Hello @Doc1faux! Thank you for your response.

Here's my complete code snippet for Android and yes I always call that Crisp.resetChatSession() when opening the crisp view

 private void initCrispChat(Context context, UserProfile profile) {
        Crisp.resetChatSession(context);
        if(profile == null){//anonymous user
            Crisp.setTokenID(context, user.userID);
            Crisp.setSessionString("user_id", user.userID);
            Log.d("crisp_logger", "ANONYMOUS USER ID: " + user.userID);
        }else {
            Crisp.setTokenID(context, profile.getUserId());
            Log.d("crisp_logger", "SessionId: " + Crisp.getSessionIdentifier(context) + " Token Id:" + profile.getUserId());
        }
        isCrispChatActive = true;
        startActivity(new Intent(context, im.crisp.client.ChatActivity.class));
    }

The issue we encountered is intermittent sometimes its working properly but there's a chance we encountered it randomly. Do you have any suggestion about it?

Doc1faux commented 1 month ago

if you call Crisp.resetChatSession(context) before calling Crisp.setTokenID(context, tokenID), it should work indeed. When you call Crisp.setTokenID(context, tokenID), are you sure the chatbox is not opened? It is ignored in this case. Do you use an unmodified Activity or Application Context? One of our customers got an issue recently where he cannot join the chat, so it's worth checking. Are you working with one or more websiteID? Keep in mind that Crisp.getSessionIdentifier(context) returns the sessionId only when the chatbox is opened and the session:joined event received from socket, it returns null otherwise.

reygie25 commented 1 month ago
  1. When you call Crisp.setTokenID(context, tokenID), are you sure the chatbox is not opened? It is ignored in this case Answer: Yes, as you can see in the code snippet the startActivity is at the bottom, so the chat box is not yet opened when calling the Crisp.setTokenID.

  2. Do you use an unmodified Activity or Application? Answer: What do you mean by unmodified activity or application? can you enlighten me about this or give us an example?

  3. Are you working with one or more websiteID? Answer: Yes, we're using a different websiteID for each account, and we only encountered this issue on a TRIAL account. Please check the image below. image

  4. Keep in mind that Crisp.getSessionIdentifier(context) returns the sessionId only when the chatbox is opened and the session:joined event received from socket, it returns null otherwise. Thank you for this clarification, that's why it always returns a null value

Doc1faux commented 3 weeks ago

Hi @reygie25, sorry for getting back to you late,

About the Activity or Application Context, one of our customer said:

there was a function overriding the Context of the AppContext into a Locale configured version of the Context. This made your chat not load correctly.

I'll check if I got the issue too with a trial website.