You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic Cordova如何检测滑动清除最近应用时的APP杀死事件?

Solution for SIP.js Call Hangup When App Is Killed (Android & iOS)

Hey there, let's tackle this tricky problem you're facing with your SIP.js VoIP app. Since neither Android nor iOS gives us a direct way to catch the app-kill event when swiping from recent apps, we need to use platform-specific workarounds combined with SIP-level mechanisms to ensure the remote call gets terminated properly.

Android Workarounds

Use a Foreground Service with onTaskRemoved()

Android provides the onTaskRemoved() callback in a Service class, which triggers when the user removes your app from the recent apps list. This is your best bet to send a SIP BYE request before the process is fully killed.

Steps to implement:

  1. Create a foreground service that runs while the call is active. This prevents the system from killing the service immediately when the app goes to background.
  2. Override onTaskRemoved() in your service to initiate the call hangup using SIP.js:
class CallForegroundService : Service() {
    private lateinit var sipUserAgent: UserAgent // Your SIP.js UA instance

    override fun onTaskRemoved(rootIntent: Intent?) {
        super.onTaskRemoved(rootIntent)
        // Send SIP BYE to terminate the call
        sipUserAgent.terminate()
        stopSelf()
    }

    // Other service setup methods (startForeground, onBind, etc.)
}
  1. Register the service in your AndroidManifest.xml and declare required permissions:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <!-- For Android 12+ -->

<service android:name=".CallForegroundService" />

Note: You must show a persistent notification while the foreground service is running (required by Android system rules). Make this notification call-aware, with options to hang up directly if needed.

iOS Workarounds

iOS doesn't expose a direct app-kill event, but we can use background task windows and server-side SIP mechanisms to mitigate this:

Request Background Task Time on App Background

When the app enters the background (before it gets killed), request extra background runtime to send a SIP BYE:

import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {
    var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid
    var sipUserAgent: UserAgent? // Your SIP.js UA instance

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Request background task to handle call cleanup
        backgroundTaskID = application.beginBackgroundTask(withName: "CallCleanup") {
            // Expiration handler: cleanup if we run out of time
            self.endBackgroundTask()
        }

        // Send SIP BYE immediately
        sipUserAgent?.terminate()

        // End the background task once done
        endBackgroundTask()
    }

    private func endBackgroundTask() {
        UIApplication.shared.endBackgroundTask(backgroundTaskID)
        backgroundTaskID = .invalid
    }
}

Keep in mind: iOS only grants ~30 seconds of background runtime here, so prioritize sending the BYE request first.

Combine with VoIP Push Notifications & Server-Side Heartbeats

For more reliability, configure your SIP server to send VoIP push notifications if the client becomes unresponsive. Additionally:

  • Enable SIP keep-alive in your SIP.js configuration to send regular OPTIONS/REGISTER packets:
const ua = new UserAgent({
  // Other configs
  keepAliveInterval: 30, // Send keep-alive every 30 seconds
});
  • Set a short session timeout on your SIP server. If the server stops receiving keep-alive packets from the killed app, it will automatically terminate the call and send a BYE to the remote party.

Universal SIP-Level Fix

No matter the platform, relying on server-side session management is critical for edge cases where the client can't send a BYE (e.g., sudden app crash, network loss):

  • Configure your SIP server to detect unresponsive clients via keep-alive timeouts.
  • Set a reasonable session expiration window (e.g., 90 seconds) so stale calls are cleaned up automatically.

内容的提问来源于stack exchange,提问作者Ivan Domnich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:37:50