amazingbasil
amazingbasil

Reputation: 1715

How to check is app in foreground from service?

I need to show notification to user only if application is not in foreground. Here is my public class MyFirebaseMessagingService extends

FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if(applicationInForeground()) {
            Map<String, String> data = remoteMessage.getData();
            sendNotification(data.get("title"), data.get("detail"));
        }

    }

need to implement applicationInForeground() method

Upvotes: 24

Views: 15428

Answers (4)

bompf
bompf

Reputation: 1514

You can use ProcessLifecycleOwner from Jetpack lifecycle components.

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner

class AppFirebaseMessagingService : FirebaseMessagingService(), DefaultLifecycleObserver {

    private var isAppInForeground = false

    override fun onCreate() {
        super.onCreate()

        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
    }

    override fun onDestroy() {
        super.onDestroy()

        ProcessLifecycleOwner.get().lifecycle.removeObserver(this)
    }

    override fun onStart(owner: LifecycleOwner) {
        isAppInForeground = true
    }

    override fun onStop(owner: LifecycleOwner) {
        isAppInForeground = false
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (isAppInForeground) {
            // do foreground stuff on your activities
        } else {
            // send a notification
        }
    }
}

See here how to import the necessary dependencies, since lifecycle is not part of the standard Android SDK.

Upvotes: 20

Khal91
Khal91

Reputation: 167

I just report a Kotlin version of @bompf answer

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class AppFirebaseMessagingService : FirebaseMessagingService(), LifecycleObserver {

    private var isAppInForeground = false

    override fun onCreate() {
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        ProcessLifecycleOwner.get().lifecycle.removeObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onForegroundStart() {
        isAppInForeground = true
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onForegroundStop() {
        isAppInForeground = false
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (isAppInForeground) {
            // do foreground stuff on your activities
        } else {
            // send a notification
        }
    }
}

Upvotes: 4

Batuhan Coşkun
Batuhan Coşkun

Reputation: 2969

You can control running app processes from android system service. Try this:

private boolean applicationInForeground() {
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> services = activityManager.getRunningAppProcesses();
    boolean isActivityFound = false;

    if (services.get(0).processName
            .equalsIgnoreCase(getPackageName()) && services.get(0).importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
        isActivityFound = true;
    }

    return isActivityFound;
}

Good luck.

Upvotes: 27

Doug Stevenson
Doug Stevenson

Reputation: 317808

At Google I/O 2016, I gave a talk where one of the topics was how Firebase detects if your app is in the foreground. You can use ActivityLifecycleCallbacks for that by incrementing a counter for every activity in your app that gets started, then decrementing it for each activity that gets stopped. If the counter is > 1, then your app is in the foreground. The relevant part of the talk can be seen on YouTube here.

Upvotes: 22

Related Questions