Reputation: 87
let content = UNMutableNotificationContent()
content.badge = 0 // your badge count
This code is in applicationWillEnterForeground
and the purpose is to erase the badge by setting the number to 0 wen the user re-enters the app however it has proven totally useless as it does not update the badge on the app icon ...
I have now reverted to iOS 9 code and it's working like a charm. Furthermore it seems that, using the new framework, you have to actually send a notification for the badge number to be updated which is really impractical .. someone please correct me if I am wrong
Furthermore:
let content = UNMutableNotificationContent()
TableViewController.numberBadges += 1
content.title = "Title"
content.body = "this is the body ..."
content.badge = TableViewController.numberBadges as NSNumber?
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let requestidentifier = "myNotification"
let request = UNNotificationRequest(identifier: requestidentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: {error in
// handle error if there is one
if((error) != nil){
print("Error completing notification scheduling: \(error)")
}else{
print("Added Notification request successfully with \(content.badge!) badges")
}
})
does NOT update the badge so what gives?
Upvotes: 3
Views: 1305
Reputation: 111
Setting badge = 0 seems broken as you say.
But as to your second question... it appears that the problem is related to your content.body and .title. That will cause a notification to show up in the trey. If you clear this before running again, the badge should update each time.
But if you leave the old notification in the trey, it appears to collapse the new one into the old one and not update the badge the second time. Seems like another bug.
Obviously the only robust way of handling this would be to send badge updates separately from text notifications (i.e. remove the .body and .title) - seems to work fine when badge != 0.
Upvotes: 0
Reputation: 81
On both iOS 10 and 11 erasing the badge by scheduling a notification with UNNotificationRequest
has issues. The documentation says setting the badge
property of the UNMutableNotificationContent
object to 0 erases the badge when the notification is triggered, but this doesn't seem to work. Through experimentation I've found workarounds for this bug in both iOS versions.
On iOS 10 / Xcode 8, as you observed, setting the value to 0 this doesn't work - the badge continues to display its current value. I discovered that if you set it to -1:
let content = UNMutableNotificationContent()
content.badge = -1
then it does erase the badge when the notification is triggered.
On iOS 11 / Xcode 9 setting the badge property to any value less than 1 does nothing - the badge continues to display its current value. I discovered that if you set the badge to 0 and also set the sound property, such as:
let content = UNMutableNotificationContent()
content.badge = 0
content.sound = UNNotificationSound.default()
then the badge is erased. In my app I've requested permission, using the requestAuthorization
method, to display the badge, but I didn't request permission to play a sound, so no sound is actually played when the notification is triggered. (If your app does request permission to play sounds, and you don't want to play one when you erase the badge, I don't know what the workaround is.)
UPDATE: I've also checked via the simulator that the workaround for iOS 11 also works on iOS 10.
I haven't had any other issues (yet!) scheduling notifications which update the badge, as long as the badge
property is set to a positive number.
Upvotes: 2
Reputation: 11
I had the same problem until I cast content.badge as non-optional.
Upvotes: 1