flutterwebrtcsipphone-callbackground-service

Flutter receive web rtc calls when app is terminated (detached state)


Websocket connection is closed abruptly in asterisk using Web RTC connection when my flutter app terminated (User swipe to close app)

Flutter version

Flutter 3.24.0 • channel stable • https://github.com/flutter/flutter.git Framework • revision 80c2e84975 (3 months ago) • 2024-07-30 23:06:49 +0700 Engine • revision b8800d88be Tools • Dart 3.5.0 • DevTools 2.37.2

Flutter library used

sip_ua: https://pub.dev/packages/sip_ua

Asterisk version

Asterisk 20.4.0

I tried using flutter background service to open the connection as soon as the connection is closed:

@pragma('vm:entry-point')
  static void onStart(ServiceInstance service) async {
    print("call background serivce is starting");
    SipUAListenerBackground newListener = SipUAListenerBackground();
    SipHelperManager().getHelper().addSipUaHelperListener(newListener);
  }

and for my SipUAListenerBackground:

class SipUAListenerBackground implements SipUaHelperListener {

@override
  void transportStateChanged(TransportState state) {
    // If state is closed
    // ...
    UaSettings settings = UaSettings()
          ..webSocketUrl = savedLoginInfo['websocketUrl']
          ..uri = 'sip:${savedLoginInfo['username']}@${savedLoginInfo['server']}'
          ..authorizationUser = savedLoginInfo['authorizationUser']
          ..password = savedLoginInfo['password']
          ..displayName = savedLoginInfo['displayName']
          ..userAgent = 'Flutter SIP Client'
          ..dtmfMode = DtmfMode.RFC2833;

     await _sipHelper!.start(settings);
  }
}

So is there anyway to keep my websocket connection alive to listen for new incoming call even when the app is terminated? Any help would be appreciated


Solution

  • After doing some research i found that there is no way for me to keep the websocket connection alive after the app is terminated as stated here make sure the session terminates

    So the only way for me to keep it alive is initial it with in the background service as:

      @pragma('vm:entry-point')
        static void onStartForeground(ServiceInstance service) async {
            SipHelperManager().getHelper().start(settings); --> Start websocket connection
        }
    
    //and then using it in flutter background service configuration:
    
    static Future<void> initializeService() async {
        final service = FlutterBackgroundService();
    
        await service.configure(
          iosConfiguration: IosConfiguration(
            autoStart: true,
            onForeground: onStartForeground,
            onBackground: onIosBackground,
          ),
          androidConfiguration: AndroidConfiguration(
            autoStart: false,
            onStart: onStartForeground,
            isForegroundMode: true,
            autoStartOnBoot: false,
            initialNotificationTitle: 'Your app',
            initialNotificationContent: 'Initialing..',
            foregroundServiceTypes: [
              AndroidForegroundType.phoneCall,
              AndroidForegroundType.microphone,
              AndroidForegroundType.mediaPlayback //Adjust this type
            ],
          ),
        );
      }