fail to start activity from service

Viewed 105

Before asking this question. I was looking first that anybody else also got this issue or not. But I didn't found any. Brief: I want to start a activity when a message comes via FCM. in the onMessageReceived(@NonNull RemoteMessage remoteMessage) whenever message incoming am handling with this code

Intent intent = new Intent(getApplicationContext(), IncomingMeetingConference.class);
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

it work fine if app foreground. But when I make app in background. Activity is not launching.

so far I observed that in console am getting

I/Timeline: Timeline: Activity_launch_request time:771108234

but no message for ActivityThread related.

whereas when app is in foreground I get ActivityThread log message in console. Please let me know how to solve this issue?

2 Answers

You cannot do that. This behaviour is discouraged. You are not allowed to interrupt the user when you get a message from the server. The correct behaviour is to show a Notification. The user can then tap on the Notification to view the Activity. This allows the user to decide when and if he wants to see it.

Thanks David for giving me right idea to do it. so i found how a code to check app is in background or foreground by

public boolean foregrounded() {
    ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo();
    ActivityManager.getMyMemoryState(appProcessInfo);
    return (appProcessInfo.importance == IMPORTANCE_FOREGROUND || appProcessInfo.importance == IMPORTANCE_VISIBLE)
}

if app is in foreground it will return true. If app in background it will return false.

Related