applicationIconBadgeNumber不显示

本文总结了iOS应用中徽章数字不显示的原因,并提供了在iOS 8及以上的系统版本中如何正确设置徽章数的方法,包括注册支持用户交互的类型等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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 sharedApplicationregisterUserNotificationSettings:mySettings];
//  如果是远程通知的话,则还需注册消息推送
[[UIApplication sharedApplication] registerForRemoteNotifications];

对于远程通知来说还必须执行注册的第二步(本地通知不需要)以获取APNs服务器用于分发通知的应用程序特定的设备令牌。

所以在使用之前先对系统加以判断,如果系统时8.0及以上则加上上述注册代码块。

float version = [[[UIDevice currentDevice] systemVersion] floatValue];

if (version >= 8.0) {

// 注册代码块

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值