2

I have googled this non stop today and could not find anything. My scenario is the following:

I have an Android app that auto replies to incoming messages. I have the below code to create a persistent (non swipe-able) notification and then when the app is destroyed via onDestroy the notification is removed. However, when I open the recents panel and swipe my app away, the app stops the auto reply service and the broadcast receiver stops, however onDestroy is not called and the notification is still visible.

public void notifCreate() {
    Intent i = new Intent(Main.this, Main.class);
    PendingIntent pi = PendingIntent.getActivity(Main.this, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder notifBuilder = new Notification.Builder(getApplicationContext());

    notifBuilder.setContentTitle(getString(R.string.app_name));
    notifBuilder.setContentText(getString(R.string.notification));
    notifBuilder.setContentIntent(pi);
    notifBuilder.setSmallIcon(R.drawable.ic_launcher);
    notifBuilder.setOngoing(true);
    Notification notif = notifBuilder.getNotification();
    notifManager.notify(1, notif);
}

public void notifDestroy() {
    NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notifManager.cancelAll();
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
    loadPrefs();
}

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

@Override
protected void onPause() {
    super.onPause();

    notifCreate();
}

@Override
protected void onResume() {
    super.onResume();

    loadPrefs();
    checkStates();
    notifDestroy();
}

@Override
public void onDestroy() {
    super.onDestroy();

    notifDestroy();
    service = false;
}

My goal is the following:

I would love to simply destroy the notification if the app is force closed etc, or if possible I would not mind the app to be a service so even when swiped from the recents, it will still run. The best scenario would be, however, for when my broadcast receiver is running (so somehow detect when it is actually working) and whenever it is on, show a notification. Whenever it is not running the notification should be wiped. If more code is needed etc just comment, thanks for any help guys.

sociallymellow
  • 155
  • 1
  • 2
  • 14

4 Answers4

2

If your notifications are handled by a service and target API is 14 or higher then you have an opportunity to cancel notifications in overriden "void onTaskRemoved(Intent rootIntent)" method.

Ruslan Yanchyshyn
  • 2,774
  • 1
  • 24
  • 22
1

As far as i can tell you are creating a new NotificationManager object to create the notification and a different notificationManager object to cancel the notifications. You should be using the same object. The way you are doing it, you are not firing any notification with the object in the notifDestroy(), hence probably it doesnt clear the notification for you.

user2511882
  • 9,022
  • 10
  • 51
  • 59
  • Would you mind giving me an example of how to do this? The way I did it was the only way I knew how. Thanks (: – sociallymellow Feb 09 '14 at 03:47
  • All i am saying use the same object you used to create the notification in the notifDestroy as well. Remove NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); from the notifDestroy method and make the notifManager a class level variable, so that all the methods can access it – user2511882 Feb 09 '14 at 03:51
  • Okay, that's what I thought you meant but just making sure, I will try this real fast and if it works I will accept your answer, thanks again for the help. – sociallymellow Feb 09 '14 at 03:57
  • And the notification remains in the status bar even if you close the app from the recent panel. That is because you might be registering the broadcast receiver in the manifest. – user2511882 Feb 09 '14 at 04:11
  • Should I not register the broadcast receiver in the manifest? – sociallymellow Feb 09 '14 at 04:18
  • http://stackoverflow.com/questions/12997800/cancel-notification-on-remove-application-from-multitask-panel.. This is will help you out. You need to dynamically register the receiver or create a service to kill the notification.. – user2511882 Feb 09 '14 at 04:25
1

Have you try .setAutoCancel(true) in your notification and in your manifest add android:launchMode="singleTask" , android;clearTaskOnLauch="true" in the activity xml

your notification icon should clear the moment you press it on the notification bar.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
1

The method that I used to kill the processes when I swipe apps from the recents is: in the first activity of the app, in the method OnDestroy I write that "the process have to kill". In this way I call the OnDestroy method of the process and so the process really kills himself and the notification is removed. Specifically, in the first Activity:

    @Override
public void onDestroy() {
  super.onDestroy();

  Intent intent = new Intent();
  intent.setAction(Service.MY_ACTION_FROMACTIVITY);
  intent.putExtra(Service.CMD, Service.CMD_STOP);
  sendBroadcast(intent);

}

So the following operations are:

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    barNotification.cancel(NOTIFICATION);

    sensorManager.unregisterListener(this);

    this.unregisterReceiver(myServiceReceiver);
    super.onDestroy();
}

I hope that I can help you.

Grancein
  • 642
  • 10
  • 19