我正在尝试为应用图标添加徽章,但根本没有任何反应。
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
UIApplication.sharedApplication().applicationIconBadgeNumber = 40;
我在这段代码中遗漏了什么吗?
答案 0 :(得分:5)
您是否要求获得更改徽章的许可?这在iOS8中已更改,需要特定权限:
let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge,
categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
答案 1 :(得分:0)
注册远程通知
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge,
categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
return true
}
更新您的徽章编号
UIApplication.sharedApplication().applicationIconBadgeNumber = 40;
答案 2 :(得分:0)
以前的答案都很旧。
iOS 10及更高版本:
let options: UNAuthorizationOptions = [.alert, .sound, .badge];
UNUserNotificationCenter.current()(options: options) { (granted, error) in
//print("granted: \(granted)")
if let error = error {
print("Error requestAuthorization: \(error.localizedDescription)")
}
}