尝试使用此代码通过远程通知设置徽章编号
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
[application setApplicationIconBadgeNumber:7];
..
}
但我没有在图标上看到徽章。
是否有我遗失的配置?
答案 0 :(得分:4)
徽章:要显示为应用图标徽章的数字。 如果此属性不存在,则不会更改徽章。要删除徽章,请将此属性的值设置为0。
您不需要手动设置徽章。如here中所述,有效负载根据"徽章"自动管理它。在你的有效载荷中。你是谁在服务器端代码中管理有效载荷。
这样的事情:
{
"aps" : {
"alert" : {
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
},
"badge" : 5,
},
"acme1" : "bar",
"acme2" : [ "bang", "whiz" ]
}
此外,首先您必须在iOS8中向用户请求权限,例如-didFinishLaunchingWithOptions
委托方法中的this
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
如果您的应用收到通知,然后您的应用将变为有效状态,则您有责任重置徽章编号,如下所示:
- (void)applicationDidBecomeActive:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
}
这是下面引用的另一个reference:
徽章图标编号由作为推送消息接收的有效负载中的徽章属性控制。在应用程序变为活动状态之前,应用程序本身无法播放显示的数字。设置'徽章'属性为任何整数值(注意:它不应该是字符串,不应该包含在"")做需要的事情。接收通知的操作系统会查找“徽章”的值。并立即将其设置在应用徽章图标上。当应用程序未激活时,会发生这一切。为了确保它根据应用程序中的内容递增或递减,应用程序应使用更新的徽章编号向服务器发送消息。
在活动状态下,应用程序负责处理遥控器 通知并更改徽章图标。