-1

I want to show local notification in an Android app that i am working. notification are based on network transaction completed in background.

I have tried service, intent service, jobservice etc but nothing is working when the app is closed.please share some working code for the reference.....

//Here is my service----

public class JobSyncFAQ extends Service {

private Message msg;
    private Looper mServiceLooper;
    private ServiceHandler mServiceHandler;

public JobSyncFAQ() {
}

private final class ServiceHandler extends Handler {
    public ServiceHandler(Looper looper) {
       super(looper);
    }
    @Override
    public void handleMessage(Message msg) { 
        // here is my stuff
        stopSelf(msg.arg1);
    }
 }
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("JobSyncFAQ", Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
mServiceHandler.removeCallbacks(mServiceLooper.getThread());
mServiceLooper.quit();
Intent intent = new Intent(getApplicationContext(),JobSyncFAQ.class);
            sendBroadcast(intent);
}

//and in my receiver 

public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, JobSyncFAQ.class));
}

please note - i am facing this issue only for N+ version

  • As I Understand your question, your main issue is not that the notification is not showing but rather that your background service is not running, am I right? If so, what are you trying to do in the background and did you verify that your actual task is being executed? – vatbub Jun 07 '18 at 08:25
  • yes service getting started when my app initialised.... but when i close the app... nothing happens... i am broadcasting intent on ondestroy with the applicationcontext – Chandrashekhar Tayade Jun 07 '18 at 08:31
  • So, your service is running, but when you close the app, the service keeps running? – vatbub Jun 07 '18 at 08:32
  • yes until its work done... after that... nothing... – Chandrashekhar Tayade Jun 07 '18 at 08:33
  • I posted an answer, but please edit your question and add the relevant code (i. e. the code of your service implemimplementation). Otherwise, I cannot post code samples specific to your case. – vatbub Jun 07 '18 at 08:43

1 Answers1

2

When you "close" your app (i. e. you swipe it away on the recent apps screen), you don't actually close the app, you only close the underlying activity. In most cases, this corresponds to the app being closed, but if you have a background service running, the service is not terminated. This is for a good reason: YOu still want to receive WhatsApp messages, even when WhatsApp is closed. Hence, WhatsApp starts a background service to check for new messages, even when the app is closed.

For that reason, you need to notify your background service about the fact that it should cancel its task.

I developed a media application that uses a MediaBrowserService which is connected to a MediaController. Therefore, I can do the following in my activities onDestroyMethod:

MediaControllerCompat mediaControllerCompat = MediaControllerCompat.getMediaController(getActivity());
if (mediaControllerCompat != null)
    mediaControllerCompat.getTransportControls().stop();

This tells the MediaBrowserService to stop and quit.

Edit: Since you use a generic service, you need to call stopService:

stopService(new Intent(MyActivity.this, JobSyncFAQ.class));
vatbub
  • 2,713
  • 18
  • 41