Android losing websocket connection in foreground service

Viewed 233

I am using a websocket connection to my server to receive/send specific messages. There messages also comes with a Notification.

Everything works fine when the connection is made. The Push Notification shows up when i send a message from the server to the App and everything works fine, but when i try it again after 30 minutes or longer and send a Message from my server to the App nothing happens. From what i can see in the logs both the App and the server socket connection is not disconnected :/ .

Is there a reason why this happening?

Do I have to ping every minute to the server to keep the connection up and running?

This is the code that I am using to start the Service.

MainActivity

...
    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val myServiceIntent = Intent(this, ForegroundSocket::class.java)
            ContextCompat.startForegroundService(this, myServiceIntent)
    }
...

ForegroundSocket

...
    // Binder given to clients (notice class declaration below)
    private var notification: Notification? = null
    private val mBinder: MyBinder = MyBinder()

    // Channel ID for notification
    val CHANNEL_ID = "Random number notification"

    /**
     * Class used for the client Binder. The Binder object is responsible for returning an instance
     * of "MyService" to the client.
     */
    inner class MyBinder : Binder() {
        // Return this instance of MyService so clients can call public methods
        val service: ForegroundSocket
            get() =// Return this instance of MyService so clients can call public methods
                this@ForegroundSocket
    }

    /**
     * This is how the client gets the IBinder object from the service. It's retrieve by the "ServiceConnection"
     * which you'll see later.
     */
    override fun onBind(intent: Intent): IBinder? {
        return mBinder
    }

    /**
     * Called when service is created So  we will do our work here
     */
    override fun onCreate() {
        super.onCreate()
        Log.d("MyBoundService", "onCreate called")
        startNotification()
    }

    /**
     * Used for creating and starting notification
     * whenever we start our Bound service
     */
    private fun startNotification() {
        val channel = NotificationChannel(
            CHANNEL_ID,
            "My Channel",
            NotificationManager.IMPORTANCE_DEFAULT
        )

        (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(
            channel
        )
        notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_lock_outline)
            .setTicker("Ticker text")
            .setContentTitle("A service is running in the background")
            .setContentText("Generating random number")
            .build()
        startForeground(1, notification)
    }

}
...
2 Answers

I don’t know where the Binder base class comes from but a websocket implementation should provide onClose

override fun onClose(code: Int, reason: String?, remote: Boolean) {
    Log.d(TAG, "onClose")
    unsubscribe()
}

If onClose is unexpected(not closed by client), then you should try to reconnect.

In this way you use bound services and inbound services, when activity is destroyed, the services are destroyed, too. So unbounded these services by returning null on bind.

Related