Home > Enterprise >  How To Cancel a Notification When Android App is Closed By Swipe?
How To Cancel a Notification When Android App is Closed By Swipe?

Time:12-07

When you close the app by swiping it in recent apps, it will cancel any services and terminate most aspects of the app gracefully. However, if there are any notifications that were SetOngoing(true), then these will remain if the app suddenly is closed, and there aren't any services that listen for the app's termination.

What is the right way to deal with this problem?

Recently, I coded a music player, and I arranged it such that in the OnStop for my activities, the notification is canceled (and so is the thread updating the progress bar within it). Then, OnResume, I trigger the notification again.

If they "recent apps swipe" it away, or click away, the notification goes away now, as long as the music isn't playing. So to get rid of the notification, you have to pause it, and then swipe away. Otherwise, there is a leak memory if the app is closed by swipe, where the notification remains open and is buggy afterwards if the app is reopened, and the app crashes if you click the notification (though maybe that's because I can't figure out how to get started with saved state bundles). Likewise, there is a problem if you let the app close the notification every OnStop, as then it will be closed as the user does other things with their phone, even though the music is playing (which sort of defeats the point of it right?)

Are there other better ways to handle this? Who has a good saved state bundle if that is indeed relevant to my issue?

Thanks for the discussion

CodePudding user response:

You can cancel the notification when android App is closed by swipe with the following code:

      [Service]
      public class ForegroundServiceDemo : Service
      {
           
            public override void OnTaskRemoved(Intent rootIntent)
            {
                  //this.StopSelf();
                  //this.StopForeground(StopForegroundFlags.Remove);
                  this.StopService(new Intent(this,typeof(ForegroundServiceDemo)));
                  base.OnTaskRemoved(rootIntent);     
            }
           
      }

By overriding the OnTaskRemoved method of the service, the system will call this method when user closes the app by swipe. And each of the three lines code can cancel the notification and stop the service when the app is closed by swipe.

CodePudding user response:

I found this, finally, after trying every search terms imaginable, and wow there is a whole section on this. I do not have it working yet, but I can report back with code when I do. Here is the solution: https://developer.android.com/guide/topics/media-apps/media-apps-overview

Seems you have to implement the media player service as a specific kind of service that registers to the notification. I am in the process of refactoring the heart of my code, which perhaps should be terrifying, but feels more like the final algorithm on a Rubix's cube... I will report back in like 10 work hours with some working code (I hope).

Thanks to everyone contributing on this discussion!

  • Related