I use NotificationManager, NotificationChannel and NotificationCompat.Builder to display a push notification when an event is triggered, no online Firebase.
The push notification appears when my app is in foreground and background, but it only should appear when my app in the background.
I have 2 Activities where just only 1 knows the class, where the push notification is created. I could just set a variable that shows if my Activity is in foreground or not (with onResume and onPause), but if my other Activity starts the Activity with the notification trigger class is set in background and therefore I can't use this method. I would need to know if the whole app is in foreground or not, but I didn't find a good solution for that problem.
So is there an other way to display push notifications just when my whole app is in background?
So I found a proper and very good solution for this problem. According to this answer: https://stackoverflow.com/a/42679191/6938043
Application.ActivityLifecycleCallbacks
public class MainActivityChats extends AppCompatActivity implements Application.ActivityLifecycleCallbacks
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getApplication().registerActivityLifecycleCallbacks(this);
}
public void onActivityResumed(Activity activity)
{
if(msg_updater != null)
{
msg_updater.set_app_is_active(true);
}
}
public void onActivityPaused(Activity activity)
{
if(msg_updater != null)
{
msg_updater.set_app_is_active(false);
}
}
public void onActivityStopped(Activity activity) {}
public void onActivityCreated(Activity activity, Bundle bundle) {}
public void onActivityStarted(Activity activity) {}
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}
public void onActivityDestroyed(Activity activity) {}
}