A.Danibo
A.Danibo

Reputation: 624

Foreground service content intent not resuming the app but relaunching it

I've been browsing many topics about resuming an activity from a foreground service without finding any concrete answer to my problem.

I'm trying to put a foreground service in my app, and I want the app to be resumed when clicking on the service notification instead of relaunching it. I've tried using the getLaunchIntentForPackage() method from PackageManager, which is the closest to what I want to do.

However, the activity's onCreate is still being called when resuming the app by clicking on the notification.

So here is my question, how to resume an app from a notification's content intent?

I'm starting my ForegroundService in the activity's onStop so it gets called when the app is killed or sent to background.

override fun onStop() {
    super.onStop()
    Log.v(TAG, "onStop")
    ForegroundService.startService(this, "Hellooooooo, here is the background")
}

ForegroundService

class ForegroundService: Service() {

    companion object {
        private const val CHANNEL_ID = "ForegroundServiceChannel"

        fun startService(context: Context, message: String) {
            val startIntent = Intent(context, ForegroundService::class.java)
            startIntent.putExtra("inputExtra", message)
            ContextCompat.startForegroundService(context, startIntent)
        }
        fun stopService(context: Context) {
            val stopIntent = Intent(context, ForegroundService::class.java)
            context.stopService(stopIntent)
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val input = intent!!.getStringExtra("inputExtra")

        val launchIntent = packageManager.getLaunchIntentForPackage(APP_PACKAGE)
        val contentIntent = PendingIntent.getActivity(applicationContext, 0,
            launchIntent, 0)

        val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Foreground Service")
            .setContentText(input)
            .setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ic_call_to_action)
            .setOngoing(true)
            .build()

        startForeground(1, notification)
        createNotificationChannel()

        return START_NOT_STICKY
    }

    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val serviceChannel = NotificationChannel(
                CHANNEL_ID,
                "Foreground Service Channel",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val manager = getSystemService(
                NotificationManager::class.java
            )
            manager?.createNotificationChannel(serviceChannel)
        }

    }

}

Upvotes: 1

Views: 383

Answers (1)

Sergii.PSP
Sergii.PSP

Reputation: 71

  1. Try set in the manifest for activity android:launchMode="singleInstance".
  2. Do not forget set some your action to your activity intent: activityIntent.setAction(ACTION_STARTED_FROM_NOTIFICATION);
  3. Override onNewIntent in the activity.
  4. in onCreate and in onNewIntent do check if(ACTION_STARTED_FROM_NOTIFICATION.equalsIgnoreCase(intent.getAction())) { do what you need }

Upvotes: 0

Related Questions