question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Notification stays even after swiping app away with app switcher

See original GitHub issue

Device

Samsung S10+ running Android 11

🐛 Bug Report

Thanks for this great work.

The notification won’t go away when CrossMediaManager.Current.Stop(); is called. The song stops, but the notification stays with play icon as shown on screenshot below. Even after the app is swiped away with android task switcher, the notification stays. The only way to make the notification go away is as follows.

  1. Swipe out the notification
  2. by force stopping (killing) the app from installed apps => app => settings in android
  3. by restarting the device.

Ironically, clicking the play icon on the orphaned notification restarts the song again from the first song of the playlist and it works as if the app was relaunched. Not sure if this is a new normal behaviors for Android 11. I started to have this issue since Android update. I also tried to remove the notification manually by using NotificationManager.ClearAll() but don’t have any effect.

I noted that the media browser service never stops after Stop() call as asserted in this line. So, the MediaBrowserService implementation never gets to hit its OnDestroy block in this case. As a result the foreground notification stays along with the foreground media browser service.

I tried to debug the issue spending most of the time today, but its beyond me.

Expected behavior

The notification should disappear as soon as Stop() is called or when app is swiped out.

Reproduction steps

Please find sample repository here to reproduce this issue. The repository is just a basic single page Xamarin.Forms app created with visual studio 2019.

Relevant code added to the default blank app is in below two lines, result is shown in screenshot above.

https://github.com/justcoding121/xamarin-sandbox/blob/main/Xamarin.SandboxApp/Xamarin.SandboxApp/Xamarin.SandboxApp.Android/MainActivity.cs#L26

https://github.com/justcoding121/xamarin-sandbox/blob/main/Xamarin.SandboxApp/Xamarin.SandboxApp/Xamarin.SandboxApp/MainPage.xaml.cs#L19

Configuration

Version: 1.0.8

Platform:

  • 📱 iOS
  • 🤖 Android
  • 🏁 WPF
  • 🌎 UWP
  • 🍎 MacOS
  • 📺 tvOS
  • 🐒 Xamarin.Forms

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
andresguticecommented, May 5, 2021

Hi, I had the same problem, I suggest you do the following:

In your MainActivity when MediaManager starts, catch the MediaManager state change event to ensure that when the player stops it removes the notification.

CrossMediaManager.Current.Init(this);
            
CrossMediaManager.Current.StateChanged += (sender, e) =>
{
    if (e.State == MediaManager.Player.MediaPlayerState.Stopped)
    {
        var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
        notificationManager.CancelAll();

    }
};
2reactions
justcoding121commented, Apr 15, 2021

Yes. You can take the code from this repository and include in your project with few changes.

Everything inside Platform folder will go to individual platform (Android, iOS, UWP) startup project. Remaining code can be put inside a shared .net standard 2.0/2.1 class library project. You will need to make few changes in the CrossMediaManager class , so that the correct MediaManagerImplementation is provided based on the platform. Make sure you have all the platform specific Nuget dependencies in your csproj files.

If you follow the code in official google media player example app (UAMP), specifically its media browser service implementation, you should be good for Android 11 (you may ignore cast feature, which I use, but you probably don’t need). Android 11 has Playback resume feature as documented in developer website.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android, cancel notification when app is removed from " ...
The problem I have is in the background, and the user removes the app through "Recent apps" (i.e., he swipes it away), the...
Read more >
Notification bubble on Android should remain even after ...
Notification bubble on Android should remain even after banner is swiped away. And have them disappear when the notification is dealt with in ......
Read more >
How to get rid of a permanent notification on Android
First, press-and-hold on the persistent notification you want to remove. Another option is to swipe the notification left or right, and then tap...
Read more >
Notification Controls can't be swiped away or clos...
Every music app I use either lets me swipe away the controls notification, or gives me the option to close it from the...
Read more >
Control notifications on Android
Notifications show when you swipe down from the top of your screen. ... Depending what notifications you want, you can change settings for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found