Search code examples
androidandroid-asynctaskbackgroundjobservice

Cannot access Internet in Job Service When App is closed


So I wrote this piece of Job Service Class that is designed to access the Internet on Background, download few stuffs, and display Notification at an interval of 15 Seconds (Will increase it later to 45 Minutes, don't worry) :

import android.app.PendingIntent;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import java.util.ArrayList;
import java.util.HashMap;

public class NotificationService extends JobService {

    DownloadNotification d;

    @Override
    public boolean onStartJob(JobParameters params) {
        d=new DownloadNotification();
        d.execute(params);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        return false;
    }

    private class DownloadNotification extends AsyncTask<JobParameters,Void,JobParameters>
    {
        ArrayList<HashMap<String,String>> resN;
        ArrayList<HashMap<String,String>> resP;

        SharedPreferences sp;

        @Override
        protected void onPreExecute() {
            sp=getSharedPreferences("eTutionPro",MODE_PRIVATE);
            super.onPreExecute();
        }

        @Override
        protected JobParameters doInBackground(JobParameters... params) {
            resN=Connectivity.query("SELECT * FROM `notices` WHERE noticeID>? ORDER BY noticeID DESC LIMIT 15",""+sp.getInt("LastNotificationID",-1));
            return params[0];
        }

        @Override
        protected void onPostExecute(JobParameters pam) {
            Log.d("Thread","From Thread NOTICE RESULT : "+(resN!=null?""+resN.size():"null"));

            SharedPreferences.Editor prefEditor = sp.edit();
            if(resN!=null) {
                boolean b=true;
                for (HashMap<String, String> i : resN) {

                    if(b)
                    {
                        b=false;
                        prefEditor.putInt("LastNotificationID", Integer.parseInt(i.get("noticeID")));
                        prefEditor.commit();
                    }

                    Intent intent = new Intent(getApplicationContext(), ViewNotice.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

                    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "101")
                            .setSmallIcon(R.drawable.logo)
                            .setContentTitle(i.get("noticeTitle"))
                            .setContentText(i.get("noticeBody"))
                            .setContentIntent(pendingIntent)
                            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

                    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
                    notificationManager.notify(Integer.parseInt(i.get("noticeID")), builder.build());
                }
            }
            jobFinished(pam,false);
        }
    }
}

Everything works fine when the App is active or in Recents.
But when the app is removed from recents, everything work as it should except that the Async Class in NotificationService Class,i.e. doInBackground in DownloadNotification cannot access Internet to download the data.
The custom function Connectivity.query(String,String...) returns null(as designed) when it fails to fetch data.No Problem in this class I guess since everything worked fine. Not sure what to do.


Solution

  • Sorry, it was an issue with JWT Token Verification Code, and not with above code, integrated in my REST API. Found a workaround by creating another API.