Swift - Local notification icon badge number update on deliver App is in background

本小妞迷上赌 提交于 2019-12-11 04:54:33

问题


I am trying to figure how I can update dynamically the icon badge number when a local notification is delivered. Registering the badge number upon scheduling is not an option since if I register two or more notification before any are delivered the

UIApplication.shared.applicationIconBadgeNumber // this will be zero 

will always be zero until a notification is delivered.

I can use the UNUsernotification delegate with the func

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { } 

but this function is called just if the app is active. What if the app is not active?

I've read a bit around and pretty much everyone i've read says there is no way to do such a thing! But is this even possible??

How the Apple manage the notification for reminder and calendar? are they local notification and they update the icon badge? or am I making a mistake? I believe it must be a way to update the icon badge when the local notification is delivered?

Any idea guys? can't believe Apple did not provide a way to achieve this! Thank you!


回答1:


UNMutableNotificationContent has a property call badge. You set this property before trigger de notification and that's it! Badge number property is NSNumber so it's a little tricky incrementing it by 1.

let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
        "Your Last Time Alarm!", arguments: nil)
content.body = self.userInfo["descripcion"]!
content.sound = UNNotificationSound.default  
content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + 1)

The rest is set the trigger and add the request:

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) 
  let request = UNNotificationRequest(identifier: idNotificacion, content: content, trigger: trigger) 
  let center = UNUserNotificationCenter.current()
  center.add(request) { (error : Error?) in
    if let theError = error {
      print(theError)
    } else {
       ...
    }
  }



回答2:


In order to be able to increase the badge for both repetitive and scheduled notifications, you should increase UIApplication.shared.applicationIconBadgeNumber, before configuring the notification:

UIApplication.shared.applicationIconBadgeNumber += 1

And then just simply:

let notificationContent = UNMutableNotificationContent()
                notificationContent.title = "Test"
                notificationContent.subtitle = "Test"
                notificationContent.body = "Test"
                notificationContent.sound = UNNotificationSound.default
                notificationContent.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumber

In order to reset the counter every time the user opens the app, just simply set the value of UIApplication.shared.applicationIconBadgeNumber to 0 in AppDelegate.swift like this:

func applicationWillResignActive(_ application: UIApplication) {

    UIApplication.shared.applicationIconBadgeNumber = 0
}


来源:https://stackoverflow.com/questions/46307182/swift-local-notification-icon-badge-number-update-on-deliver-app-is-in-backgro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!