Skip to content
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

The session is not recovers in the android SDK #188

Open
reygie25 opened this issue Sep 5, 2024 · 5 comments
Open

The session is not recovers in the android SDK #188

reygie25 opened this issue Sep 5, 2024 · 5 comments

Comments

@reygie25
Copy link

reygie25 commented Sep 5, 2024

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));
  1. The session will not recover and created a new session: session_6dbd6617-aa65-45eb-858a-7002647bbc43
  2. The Android app is expecting to recover the session ID: session_76f9db48-4b51-463e-8a9c-0a56e3854c21
@Doc1faux
Copy link
Collaborator

Doc1faux commented Sep 6, 2024

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
Copy link
Author

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
Copy link
Collaborator

Doc1faux commented Sep 12, 2024

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.

@Doc1faux Doc1faux added the bug label Sep 12, 2024
@reygie25
Copy link
Author

  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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants