openimsdk / open-im-sdk-flutter

IM SDK Flutter
https://openim.io
MIT License
365 stars 231 forks source link

[Issue] OpenIM SDK Does Not Run in Background #170

Open buiphukhuyen opened 1 day ago

buiphukhuyen commented 1 day ago

I am using OpenIM SDK in my Flutter project and am facing issues when the app is closed or moved to the background. The SDK is not able to maintain its connection and does not receive new messages through the listeners (onRecvNewMessage, onRecvOfflineNewMessage, etc.) when the app is not actively running in the foreground. Only when the app is reopened does the SDK start processing the events.

The SDK should ideally run in the background to process incoming messages and trigger notifications.

Steps to Reproduce:

  1. Initialize OpenIM SDK in the Flutter app.
  2. Register message listeners (onRecvNewMessage, onRecvOfflineNewMessage, etc.).
  3. Move the app to the background or close the app entirely.
  4. Send a message from another client or user to the app.
  5. Observe that the message is not processed or no notification is triggered when the app is in the background or closed.

Expected Behavior: The SDK should continue to run in the background, allowing it to receive messages and trigger notifications even when the app is in the background or closed.

Environment:

Related Code Snippet: ----class OpenIMService extends GetxService----

im.OpenIM.iMManager.initSDK(
  platformID: platformID,
  apiAddr: Get.find<AppEnvironment>().chatApiAddr,
  wsAddr: Get.find<AppEnvironment>().chatWsAddr,
  dataDir: dataDir,
  logLevel: 6,
  logFilePath: '$dataDir/openim_sdk.log',
  listener: im.OnConnectListener(
    onConnectSuccess: () {
      print('Connected successfully');
    },
    onConnecting: () {
      print('Connecting...');
    },
    onConnectFailed: (code, errorMsg) {
      print('Connection failed: $errorMsg');
    },
    onUserTokenExpired: () {
      print('Token expired');
    },
    onKickedOffline: () {
      print('Kicked off other device');
    },
  ),
);

im.OpenIM.iMManager.messageManager.setAdvancedMsgListener(
  im.OnAdvancedMsgListener(
    onRecvNewMessage: (msg) async {
      _showNotification(msg);
      _newMessageStreamController.add(msg);
    },
    onRecvOfflineNewMessage: (msg) async {
      _showNotification(msg);
      _newMessageStreamController.add(msg);
    },
    onRecvOnlineOnlyMessage: (msg) async {
      _showNotification(msg);
      _newMessageStreamController.add(msg);
    },
  ),
);

 await im.OpenIM.iMManager.login(
      token: tokenChat,
      userID: currentUserID,
    );
}

------main.dart------

// Check login 
  bool isLoggedIn = await Get.find<LocalStorageRepo>().checkLogin();

// If logged in, initialize OpenIMService
  if (isLoggedIn) {
    // Register and initialize OpenIMService
    try {
      await Get.putAsync<OpenIMService>(() async {
        var service = OpenIMService(localRepo: Get.find<LocalStorageRepo>());
        await service.init();
        return service;
      }, permanent: true);
    } catch (e) {
      print('Error initializing OpenIMService: $e');
    }
  } else {
   // Register OpenIMService without initializing it
    Get.put<OpenIMService>(
        OpenIMService(localRepo: Get.find<LocalStorageRepo>()),
        permanent: true);
  }

Questions: Is there any additional configuration or background service setup required to keep OpenIM SDK running when the app is in the background or closed?

Thanks!

buiphukhuyen commented 1 day ago

I want to ask more if I want to use FCM to send the notice, I activate FCM on Openim Server. If using flutter, how to send the FCM token from SDK Flutter to Openim?

std-s commented 1 day ago

updateFcmToken

我想问一下如果我想使用 FCM 发送通知,我在 Openim Server 上激活了 FCM。如果使用 Flutter,如何将 FCM 令牌从 SDK Flutter 发送到 Openim?