Search code examples
androidandroid-notificationsalarmmanagerbackground-process

Show notification by an Android app through service running in background checking server (volley) even when the app is closed


As stated in the question, I want a background process to run from an app (daily at 21:30) which makes a volley request to the server and display a notification depending upon the result. On clicking the notification, a specific link is opened (handled by the app).

The server request and response from the class (through async Volley) is working perfectly. The link-handler is also set up.

I did a research and is confused about the class to use. It seems, I can use:

Using AlarmManager (with the receiver tag added in the manifest), I have setup the following method called in the onCreate of MainActivity.java:

private void setAlarms()
{
    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(this, NewNewsNotification.class);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, i, 0);



    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 30);
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            1000 * 60 * 60, alarmIntent);

    if (alarmMgr!= null) {
        alarmMgr.cancel(alarmIntent);
    }

}

NewNewsNotification.java

@Override
public void onReceive(Context context, Intent intent) {
    rCtx= context;
    fetch_last_update();
}

public void fetch_last_update()
{
    VolleyCallback();
    VolleyService = new AsyncJsonFetch(ResultCallback, rCtx);
    try {
        JSONObject sendObj = new JSONObject();
        mVolleyService.postDataVolley("POSTCALL", "news", sendObj);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
public void VolleyCallback()
{
    pResultCallback = new AsyncJsonData() {
        @Override
        public void notifySuccess(String requestType, JSONObject response) throws JSONException             {
            int stat = (int) response.get("status");

            if (stat == 1) {
                JSONObject msgJSON = (JSONObject) response.get("msg");
                Log.d(TAG, "msgJSON: "+msgJSON);
                /*The above log is working correctly. PROCESS THE JSON HERE AND GENERATE THE NOTIFICATION*/
            }
        }
        @Override
        public void notifyError(String requestType, VolleyError error) {
            Log.d(TAG, "Volley requester " + requestType);
            Log.d(TAG, "Volley JSON post" + "That didn't work!");
        }
    };
}

What is the correct way and how to implement it? How to initiate the clickable notification?


Solution

  • Android has newer and better solution which perfectly suits your need

    The WorkManager API is a suitable and recommended replacement for all previous Android background scheduling APIs

    you can check official topics here : work manager