sociallymellow
sociallymellow

Reputation: 155

Notification shows after android app is force closed

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.

Upvotes: 2

Views: 7377

Answers (4)

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.

Upvotes: 1

Ruslan Yanchyshyn
Ruslan Yanchyshyn

Reputation: 2784

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.

Upvotes: 2

Grancein
Grancein

Reputation: 652

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.

Upvotes: 1

user2511882
user2511882

Reputation: 9152

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.

Upvotes: 1

Related Questions