当应用程序处于最小化模式时,如何在应用程序图标上设置徽章?

时间:2012-05-31 12:47:14

标签: objective-c

当应用程序处于最小化状态并且同时发出通知时,应在应用程序图标上看到徽章。

2 个答案:

答案 0 :(得分:0)

如果通知到达并且您的应用程序不是前台,则操作系统会处理通知。

您的通知可以包含一个字段badge,以便操作系统更新徽章。但是,这意味着服务器发送通知必须知道哪个号码应该是徽章。

通知正文如下:

{
    "aps" : {
        "badge" : 9
    }
}

答案 1 :(得分:0)

继承我的解决方案。我需要在我做的应用程序中实现相同的功能。我有一个下载队列,并希望使用徽章来显示剩余的下载量,并在后台保持更新。基本上我的解决方案是,每次下载完成后,我都会设置UILocalNotification静音,而不是消息文本。只需设置徽章即可。如下所示..

- (void)queRequestFinished:(ASIHTTPRequest *)request {

    self.inResourceCount -= 1; // Deducted 1 from the total count of downloads in queue.

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {

    UILocalNotification *notif = [[cls alloc] init];
    notif.fireDate = [NSDate  date]; // Schedule notification for now.
    notif.timeZone = [NSTimeZone defaultTimeZone];
    notif.soundName = @"silence.caf";
    notif.applicationIconBadgeNumber = inResourceCount; // Number you want displayed as Badge.

    // This is where the magic happens, and actually changes your badge.    
    [[UIApplication sharedApplication] scheduleLocalNotification:notif];

    [notif release];
   }

}

我想指出,我的场景可能与你的不同。我正在使用ASIHTTPRequest库,它支持在后台运行时继续下载,并且即使在后台也会调用queRequestFinished:上面的方法。希望这有帮助,如果它确实将其标记为答案:)。