在iOS应用中经常使用UIApplication的applicationIconBadgeNumber属性来显示徽章数字(即app右上角红色数字),但有时候红色徽章数字却不显示,在此对需注意的问题加以小结。
在iOS8及以上版本,app使用本地通知和远程通知前都必须注册支持用户交互的类型
typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
UIUserNotificationTypeNone = 0, // the application may not present any UI upon a notification being received
UIUserNotificationTypeBadge = 1 <<0,// the application may badge its icon upon a notification being received,徽章
UIUserNotificationTypeSound = 1 <<1,// the application may play a sound upon a notification being received,声音
UIUserNotificationTypeAlert = 1 <<2,// the application may display an alert upon a notification being received,弹出框
} NS_ENUM_AVAILABLE_IOS(8_0)__TVOS_PROHIBITED;
对于未注册的类型,系统将会忽略该种类型的响应。常用注册代码块为:
UIUserNotificationType types =UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
// 如果是远程通知的话,则还需注册消息推送
[[UIApplication sharedApplication] registerForRemoteNotifications];
对于远程通知来说还必须执行注册的第二步(本地通知不需要)以获取APNs服务器用于分发通知的应用程序特定的设备令牌。
所以在使用之前先对系统加以判断,如果系统时8.0及以上则加上上述注册代码块。
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 8.0) {
// 注册代码块
}