How to install app icon icon badge in iOS 10?

Problem:

I am trying to install an app icon icon icon in iOS 10, but it does not work. I understand that UIUserNotificationSettings now deprecated in iOS, and UNNotificationSettings replaces it.

Question:

How to change the code below to use UNNotificationSettings to update icon badge number in iOS 10? Or is there another concise method?

The code:

The following code shows how I installed the icons from iOS 7 - iOS 9.

 let badgeCount: Int = 123 let application = UIApplication.sharedApplication() if #available(iOS 7.0, *) { application.applicationIconBadgeNumber = badgeCount } if #available(iOS 8.0, *) { application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge], categories: nil)) application.applicationIconBadgeNumber = badgeCount } if #available(iOS 9.0, *) { application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge], categories: nil)) application.applicationIconBadgeNumber = badgeCount } if #available(iOS 10.0, *) { // ????? } 
+6
source share
1 answer

You need to implement UserNotifications in AppDelegate .

 import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { 

And then use the following code inside didFinishLaunchingWithOptions :

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in if error != nil { // } } return true } 

Here you can find many important materials in the notification topic.

For the badge:

 let content = UNMutableNotificationContent() content.badge = 10 // your badge count 
+4
source

All Articles