问题:
我正在尝试在iOS 10中设置应用图标徽章编号,但它失败了。我了解UIUserNotificationSettings
现已在iOS中弃用,UNNotificationSettings
取代了它。
问题:
如何修改以下代码以使用
UNNotificationSettings
更新iOS 10中的图标徽章编号?还是有另一种简洁的方法吗?
代码:
以下代码显示了我如何设置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, *) {
// ?????
}
答案 0 :(得分:5)
您需要将UserNotifications
实施到AppDelegate
。
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
然后在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您可以在通知主题上找到大量重要内容。
对于徽章:
let content = UNMutableNotificationContent()
content.badge = 10 // your badge count