我希望在点击应该设置应用程序图标的气泡数量的按钮后看到一个气泡图标徽章编号 - 但是 - 按下homebutton后,我在图标中看不到任何编号的气泡。我的问题:图标徽章是不是出现在ios模拟器中?
- (IBAction)btn1_click:(id)sender {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:4];
}
答案 0 :(得分:3)
不完全直观,但您的应用必须首先获得处理UNUserNotificationCenter
requestAuthorizationWithOptions:completionHandler:
// import with *one of*...
#import <UserNotifications/UserNotifications.h>
@import UserNotifications;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
[UIApplication sharedApplication].applicationIconBadgeNumber = 4;
}
}];
徽章是一种与通知相关的用户互动 - 必须经过授权。
答案 1 :(得分:0)
您还应该在主线程上设置徽章-我收到上面代码的警告。
这在Swift中有效:
private func badgeTest() {
UNUserNotificationCenter.current().requestAuthorization(options: .badge) { (granted, error) in
if granted {
DispatchQueue.main.async {
UIApplication.shared.applicationIconBadgeNumber = 4
}
}
}