I'm working on small android app using MVVM
pattern.
My issue is that my ViewModel
observer in MyActivity not called from the background. I need it to be called even if the app is in background to show system Notification
to the user that app calculation process is done and the result is ready.
This is the current implementation located in onCreate
in MyActivity
:
mainActivityViewModel.getTestResult().observe(MainActivity.this, new Observer<String>() {
@Override
public void onChanged(@Nullable String blogList) {
Toast.makeText(getApplicationContext(), "test...", Toast.LENGTH_SHORT).show();
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)){
//The app is in foreground - showDialog
}else{
//The app is in background - showNotification
}
}
For now, this observer will be called only if the app is in foreground - if the process done while app was in foreground - 'showDialog' will trigger, if the app was in background - the showNotification will trigger - but only after I will open the app again. It's not the behaviour that I try to achieve. Please help! Thanks.