infobip / mobile-messaging-flutter-plugin

Mobile Messaging SDK plugin for Flutter projects
Other
9 stars 5 forks source link

How to Save User and Fetch Chat According to User ID #19

Closed ShumailaAshiqHussain closed 2 weeks ago

ShumailaAshiqHussain commented 3 weeks ago

I’ve successfully implemented Infobip Mobile Messaging for chat in my app, and it’s working perfectly. However, I’m facing an issue where chat messages are displayed according to the device, not the user. Specifically, if a user logs in on one account, chats are shown correctly. But if the user logs out and another user logs in on the same device, the previous chat messages still appear. This means that when the new user opens the chat, they see old messages from the previous account, which is not acceptable.

I need a solution to ensure that chat messages are associated with the current user ID so that each user only sees their own chat history.

From the Infobip Mobile Messaging package I found the methods saveUser, fetchUser, and getUser. I’m attempting to use the saveUser method to handle user-specific chat messages, but I’m encountering an error. Here’s the code from my InfobipService class:

class InfobipService {

  static Future<void> initializeInfobip() async {
    final configuration = Configuration(
        applicationCode: dotenv.env['infobipAppCode'] ?? '',
        inAppChatEnabled: true,
        fullFeaturedInAppsEnabled: false,
        inAppChatCustomization: mmconfiguration.InAppChatCustomization(
            toolbarTintColor: '0xFFFFFFFF'),
        defaultMessageStorage: true,
        privacySettings: mmconfiguration.PrivacySettings(
          userDataPersistingDisabled: false,
          carrierInfoSendingDisabled: false,
          systemInfoSendingDisabled: false,
        ),
        iosSettings: IOSSettings(
            notificationTypes: ['alert', 'badge', 'sound'], logging: true));

   try {
    await InfobipMobilemessaging.init(configuration);

    // Get user ID from AppData 
    String? userId = AppData().user?.userId;
    if (userId == null || userId.isEmpty) {
      log('User ID is null or empty');
      return;
    }

    UserData userData = UserData(externalUserId: userId);

    await InfobipMobilemessaging.saveUser(userData);

    await InfobipMobilemessaging.showChat();

  } catch (e) {
    log('Error initializing Infobip: $e');
    // Optionally, show an error message to the user
   log('Error initializing Infobip: $e');
  }

  }

Error: MobileMessaging API returned error (user data)! Error initializing Infobip: PlatformException(-1, Unknown error, SERVER_ERROR, -1, Unknown error, null)

Could you assist me in resolving this issue?

Looking forward to your advice.

alboldy-ib commented 3 weeks ago

Hello,

Switching between users should be performed using personalize method. SaveUser is designed to update current user with the provided data.

Please refer to these articles - personalization and profiling.

As for the unknown error - there is currently a bug on backend, which affects error response parsing on Android devices. We will fix it soon, and you will be able to see them properly as described here

alboldy-ib commented 2 weeks ago

Hello,

You have disclosed your private API key. Please delete it from here, and create a new application at Infobip Portal.

As for the personalization logic and implementation details - please contact your designated account manager and/or implementation consultant as these questions are out of technical scope.

You should not fetchUser or getUser before personalization. Personalization should be performed by some unique key only - be it externalUserId, phoneNumber, or email.

ShumailaAshiqHussain commented 2 weeks ago

ok, thank you