thudugala / Plugin.LocalNotification

The local notification plugin provides a way to show local notifications from .Net MAUI and Xamarin Forms apps .
MIT License
401 stars 66 forks source link

Badge on App Icon on iOS #488

Open Richard-Dufour opened 2 months ago

Richard-Dufour commented 2 months ago

I cancelled and cleared all my requests, yet the badge number (1) on my iPad always shows up. Even after deleting the app and re-installing it. It is stuck. I no longer have any notifications yet the badge number shows up on my app icon.

The notification clears successfully from the Notification Center, but I still have the badge number on the App icon.

Weird thing is that when I add new requests, the badge number is updated, when I delete requests, once I get to zero, I still see 1.

Also... Shouldn't the badge number property be part of Current instead of a Request? I mean, the badge is shown on a single app icon (we only see 1 icon displayed, so shouldn't the badge number always be the total number of notification requests? Feels like that property should not even have to be editable, it should just be a read only property that equals:

var current.BadgeNumber = await LocalNotificationCenter.Current.GetDeliveredNotificationList();

I am suspecting there is a bug in a loop of some sort that doesn't clear all the way down to zero.

Ex: Every time I add a request, I set the BadgeNumber to be the count of await LocalNotificationCenter.Current.GetDeliveredNotificationList();

So add1-> BadgeNumber = 1 Add one more -> BadgeNumber = 2 Add one more -> BadgeNumber = 3

Now, cancel a notification -> BadgeNumber goes to to 2 Cancel another -> BadgeNumber goes to 1 Cancel the last one, -> Badge number is stuck on 1, even weider, sometimes goes back up to 2??

How can I clear the app icon badge once I have removed all my notifications?

Running on Visual Studio 2022 Preview on a Windows 11 machine Targeted device is an iPad 10th Generation

Richard-Dufour commented 2 months ago

UPDATE: I found a work around to get the darn iOS app icon to set/clear out.

using System;

public static async void RefreshBadge()
 {
     //Called on MainPage OnAppearing and also when I click/tap on a notification
     var delivered = await LocalNotificationCenter.Current.GetDeliveredNotificationList();
     int newBadgeNumber = delivered.Count();

     //They all should have the same BadgeNumber -> The overall total number of notifications
     for (int i = delivered.Count - 1; i >= 0; i--)
     {
         var deliveredNotification = delivered[i];
         deliveredNotification.BadgeNumber = newBadgeNumber;
     }

     // Badge comes from using system, it is needed (at least on iOS) to set/clear the Badge number properly on my iPad
     Badge.Default.SetCount((uint)newBadgeNumber);
     Badge.SetCount((uint)newBadgeNumber);
 }

private void ClearAppBadgeCount()
{
    Badge.Default.SetCount(0);
    Badge.SetCount(0);
}