Search code examples
androidnotificationsalarmmanager

Android - Display notification when application is in background


I am using AlarmManager to periodically check for new content at some endpoint, validate if the results coming from the endpoint are the same as the ones I already have on my app, and if its not the same create a notification for each item.

What i need to know is how should i make the alarms to start only when the application is paused or stopped and cancel the alarms when de application is started or resumed.

where should i start the alarms and where should i cancel them?

In Android Notifications Guideline it says (on chapter: When not to display a notification):

Don't create a notification if the relevant new information is currently on screen. Instead, use the UI of the application itself to notify the user of new information directly in context. For instance, a chat application should not create system notifications while the user is actively chatting with another user.

If I have the application open i just want to disable alarms, when the application is closed/paused i want to cancel everything.


Solution

  • You need to create a Custom Application with global state and implement your own onPause and onResume at Application Level.

    Create your own subclass of Application like this:

    public class MyApplication extends Application {
    
        private static MyApplication sInstance;
    
        public MyApplication getInstance(){
            return sInstance;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            sInstance = this;
        }
    
        public void onStart() {
            // TODO: Stop your notification.
        }
    
        public void onStop() {
            // TODO: Start your notification.
        }
    
    }
    

    Specify its name in your AndroidManifest.xml's tag:

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:name="MyApplication">
    

    Create a class to hold the counts of activities:

    public class ActiveActivitiesTracker {
    
        private static int sActiveActivities = 0;
    
        public static void activityStarted()
        {
            if (sActiveActivities == 0) {
                // TODO: Here is presumably "application level" resume
                MyApplication.getInstance().onStart();
            }
            sActiveActivities++;
        }
    
        public static void activityStopped()
        {
            sActiveActivities--;
            if (sActiveActivities == 0) {
                // TODO: Here is presumably "application level" pause
                MyApplication.getInstance().onStop();
            }
        }
    }
    

    Then create a base activity (or do that in every activity), simply call the activityStarted() and activityStopped() methods:

    @Override
    public void onStart() {
        super.onStart();
        ActiveActivitiesTracker.activityStarted();
    }
    
    @Override
    public void onStop() {
        super.onStop();
        ActiveActivitiesTracker.activityStopped();
    }
    

    For more details about Custom Application, see this.

    For more details about Android Application Level Pause and Resume, see this.

    Hope this helps.