1

My app creates local notifications (using a notification manager via a dependency service) for certain things whilst it's running. However, if the user fully closes* the app, i.e. swipes it away in the overview view or whatever you call it, any notifications that still exist hang around afterward.

If the user then swipes a notification away, I get an 'app has stopped' dialog.

This is a really difficult problem to search for on the internet as no combination of search terms brings up anything other than people wanting to push notifications when the app is closed. I can't believe I'm the first person to have this problem but there is nothing at all that I can find that mentions it.

Ideally, I would simply cancel all the notifications when the app is closed, but this seems to be impossible. I've tried overriding OnDestroy() and OnStop() in MainActivity to cancel them all but it doesn't do anything (which raises the question that if this is because the app is dead at this point so no code can run then what's the point of these events?).

So, the question is: How can I cancel all the notifications when the app is closed or, failing that, how can I stop this crash when the user swipes away a notification when the app isn't running?

*I've never been able to find a definitive term for this that disambiguates it from 'backgrounding'/'sleeping' or 'killing'/'force killing'.

Robot Head
  • 396
  • 3
  • 16
  • 1
    https://stackoverflow.com/a/14374761/8892050 this link may help. I have asked similar question but no answer. I wanted to clear all notification without using service. Looks that is impossible (as i did not receive any answer for that question). https://stackoverflow.com/questions/66783758/how-to-remove-delete-all-notification-when-an-android-app-is-killed. And no reason they give down vote for the question. – Ranjit Apr 02 '21 at 05:08
  • 1
    Thanks, Ranjit. Like you I didn't want to use a service as it seems like using a sledgehammer to crack a nut. It looks like I'll have to though. I wonder if I can make the service kill itself after it's cancelled all the notifications? – Robot Head Apr 02 '21 at 08:46
  • Yes, service can kill itself after it cancelled all the notifications. – Ranjit Apr 02 '21 at 10:13
  • In the end, SushiHangover's answer in https://stackoverflow.com/questions/43793623/how-to-execute-code-when-app-is-killed-by-swiping-in-android helped me out. I remove all the tasks in the OnTaskRemoved event. – Robot Head Apr 05 '21 at 20:30

1 Answers1

1

write notification code in reciver class :

 public class MyReciver : Android.Content.BroadcastReceiver{
 public override void OnReceive(Context context, Intent intent)
 {
  if (intent.Action == "pushnotification"){
   this.SendNotification();
  }
 public void Sendnotification(){
 //notification code 
 }
 }

 }

then call reciver from service class : OnTaskRemoved method restart notification code when app is closed

 class MyServices : IntentService
{
    protected override void OnHandleIntent(Intent intent)
    {
    }
    public override void OnTaskRemoved(Intent rootIntent)
    {
        MyReciver receiver_notify = new MyReciver();
        IntentFilter intent_notify = new IntentFilter("pushnotification");
        RegisterReceiver(receiver_notify, intent_notify);
       
    }
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        
        MyReciver receiver_notify = new MyReciver();
        IntentFilter intent_notify = new IntentFilter("pushnotification");
        RegisterReceiver(receiver_notify, intent_notify);;
    }
}

then call MyServices class where you need run notification