2

Hello I have successfully been able to implement push notifications in my app however I have a slight issue I would like to fix. I have a NotificationsActivity that displays a list of all notifications received on the app.

Now if this activity is open there is simply no need for me to display push notifications at the top. So I would like a way to check if this activity is open before doing a push notification.

Is this possible? If yes how can it be done?

user3718908x100
  • 7,939
  • 15
  • 64
  • 123

1 Answers1

1

Yes it's possible. You will have to use the ActivityManager.

private boolean isActivityForeground( Class activityClass ) {

    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();

    return activityClass.getName().equals( tasks.get(0).getTaskInfo().topActivity.getClassName() );
}
HKoncept
  • 153
  • 2
  • 8