Ionic Cordova如何检测滑动清除最近应用时的APP杀死事件?
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:
- 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.
- 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.) }
- Register the service in your
AndroidManifest.xmland 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

