5

For example in WhatsApp. When I call someone a activity opens up even if the app is in background or killed. How can I implement the same? Also I don't want my application to start a new activity cause this application will be used in a controlled environment. My application will always be in the background. The users will receive notifications when the app is open (background or foreground) so when they receive this notification they app should automatically app (not start a new activity) but just push the current activity to the foreground.

To best understand this is what I am trying to implement: User A opens the app and then minimizes it. So now he is eligible to receive notifications. The application is currently on Activity A. When the user receives the notification we push the data received into a list and show it on Activity A. All I want to do is when User A receives a push notification to just push the Activity A to foreground no creating Activity A again or anything of that sort but just bring it to foreground. Any help would be much appreciated. Thank you!

Rahul Thyagaraj
  • 325
  • 5
  • 13

3 Answers3

3

You can use intent flag.

 Intent.FLAG_ACTIVITY_SINGLE_TOP 

And your code will be like

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        Intent intent= new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }
}
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53
  • 1
    You will also need the SYSTEM_ALERT_WINDOW permission in order for the activity to start from the background https://stackoverflow.com/questions/63026953/startactivity-not-working-inside-onmessagereceived-in-fcm/63027772#comment119659093_63027772 – BabyishTank May 26 '21 at 15:32
  • When I tried this just now I get "android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?" – Yossi Dahan May 22 '23 at 21:31
1

Follow this answer. You can register a broadcast in your activity, or directly call the method from your onMessageReceived()

Vihaari Varma
  • 155
  • 11
0

On message received start NotificationActivity that will check if your application already running then finish that activity will bring you last opened activity on the stack, And if app not running will start your MainActivity

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

     // Check if message contains a data payload.
       if (remoteMessage.getData().size() > 0) {
         Log.d(TAG, "Message data payload: " + remoteMessage.getData());
       }

     // Check if message contains a notification payload.
      if (remoteMessage.getNotification() != null) {
          startActivity(new Intent(this , NotificationActivity.class));
        }  
     }


public class NotificationActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        // If this activity is the root activity of the task, the app is not running
        if (isTaskRoot())
        {
            // Start the app before finishing
            Intent startAppIntent = new Intent(getApplicationContext(), MainActivity.class);
            startAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startAppIntent);
        }

      // Now finish, which will drop the user in to the activity that was at the top of the task stack
        finish();
    }
}

Check this answer, and this answer for more details.

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
  • I don’t want to start a new activity but just bring the current activity to the foreground. Cause the list the display doesn’t persist anywhere so if I keep starting activity list display will be pointless – Rahul Thyagaraj Jul 18 '18 at 04:56
  • Start `NotificationActivity` is just a trick to get app to foreground then kill opened activity will bring you back to last open list on back stack. consider to handle `onSaveInstanceState` and `onRestoreInstanceState` for your list activity – Khaled Lela Jul 18 '18 at 05:48