Löschen Sie das App -Abzeichen mit lokalen Benachrichtigungen
-
28-10-2019 - |
Frage
Ich versuche das "ungelesene" Abzeichen meiner App mit einem zu klären UILocalNotification
. Logischerweise würden Sie denken, dass dies durch Einstellen geschehen würde applicationIconBadgeNumber
von a UILocalNotification
Instanz auf 0, aber es funktioniert nicht, und die Dokumente für applicationiconbadgenumber sagen "Der Standardwert ist 0, was" keine Änderung "bedeutet."
Gibt es also keine Möglichkeit, ein Abzeichen mit lokalen Benachrichtigungen zu klären, sobald es festgelegt wurde?
Update: Ein einfacher Code:
-(void)applicationDidFinishLaunching
{
// Set the appication icon badge to 1 in 10 minutes, using a local notification so it works in the background:
// This works fine.
UILocalNotification *episodeNotification = [[UILocalNotification alloc] init];
episodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(60 * 10)];
episodeNotification.timeZone = [NSTimeZone defaultTimeZone];
episodeNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:episodeNotification];
[episodeNotification release];
// Clear the application icon badge in 20 minutes, again using a local notifcation so it works in the background:
// This doesn't work. According to the docs for local notification it's not supposed to
// because (applicationIconBadgeNumber = 0) means "Do not change the badge"
// I'm looking for an alternative if it exists.
UILocalNotification *clearEpisodeNotification = [[UILocalNotification alloc] init];
clearEpisodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(60 * 20)];
clearEpisodeNotification.timeZone = [NSTimeZone defaultTimeZone];
clearEpisodeNotification.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:clearEpisodeNotification];
[clearEpisodeNotification release];
}
Lösung
Ich hatte das gleiche Problem. Wenn das Abzeichen aus einer lokalen Benachrichtigung festgelegt wird, ist das Einstellen auf 0 der Standard für "No Change", während es direkt aus der Anwendung ausgelöst wird. Das Einstellen einer negativen Zahl durch eine lokale Benachrichtigung löste das Problem.
Versuchen:
clearEpisodeNotification.applicationIconBadgeNumber = -1;
Andere Tipps
Ja, es ist möglich, das Abzeichen aus der App selbst zu entfernen.
Ich verwende den folgenden Code in einer meiner Apps, und er funktioniert wie erwartet (dh das Abzeichen frei):
//clear app badge
[UIApplication sharedApplication].applicationIconBadgeNumber=0;