Search code examples
androidfirebaseandroid-activityandroid-service

fail to start activity from service


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?


Solution

  • 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.