2014-02-17 46 views
3

我想在发送通知时向应用程序中的UIButton添加徽章图标,但我不太确定如何实现此目的。它需要与跳板图标上的徽章相匹配,并且在应用程序中按下UIButton时,跳板徽章和UIButton上的徽章都需要消失。将推送通知徽章添加到UIButton

UPDATE:

我得到一个徽章出现,但是当我打开应用程序的徽章被重置回零的应用程序被打开时,所以我最终没有看到按钮上的徽章。我知道徽章的作品,因为我做这个测试吧:

[[UIApplication sharedApplication]setApplicationIconBadgeNumber:1]; 

下面是徽章代码:

NSString *badgeNumber = [NSString stringWithFormat:@"%d", [[UIApplication sharedApplication]applicationIconBadgeNumber]]; 

    JSCustomBadge *actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber]; 
    actionAlertBadge.frame = CGRectMake(188, 6, 30, 30); 

    [self.view addSubview:actionAlertBadge]; 

    if ([badgeNumber isEqual:@"0"]) 
    { 
     actionAlertBadge.hidden = YES; 
    } 

我怎么能有徽章得不到打开应用程序时重置,但而是在应用程序中按下按钮时重置?

我有这个工作,但仍然有一个问题。如果应用程序未在后台运行,则徽章会显示在跳板图标和UIButton上。当按钮被按下时,新的视图打开并且徽章被重置,所以当用户用UIButton返回到屏幕时,徽章不再存在。我遇到的问题是应用程序在后台运行时,徽章不会显示在按钮上,而是显示在跳板上。

- (IBAction)gotoActionAlerts 
{ 
    ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain]; 
    WebViewController *wvc = [[WebViewController alloc]init]; 
    [actionAlerts setWebViewController:wvc]; 
    actionAlerts.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [[UAPush shared] resetBadge]; 
    actionAlertBadge.hidden = YES; 
    [self.navigationController pushViewController:actionAlerts animated:YES]; 
} 

在viewDidLoad中

badgeNumber = [NSString stringWithFormat:@"%d", [[UIApplication sharedApplication]applicationIconBadgeNumber]]; 

    actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber]; 
    actionAlertBadge.frame = CGRectMake(83, 6, 30, 30); 

    [self.view addSubview:actionAlertBadge]; 

    if ([badgeNumber isEqualToString:@"0"]) 
    { 
     actionAlertBadge.hidden = YES; 
    } 
+0

我一直在想这是否可能,我需要做同样的事情! – meda

+2

是的,这是可能的。作为@Viruss的回答,你可以使用UIApplication的'applicationIconBadgeNumber'属性来设置和获取徽章。 –

回答

3

对于徽章上的UIButton可以利用现有的here一些自定义库,

并设置/让你的应用程序的徽章,你可以使用方法,

[UIApplication sharedApplication]setApplicationIconBadgeNumber:1]

编辑

The issue I'm having is when the app is running in the background, the badge doesn't show on the button, but shows on the springboard. 

如果您的应用程序在后台徽章将显示在App图标,如果您使用的是由有效载荷设置徽章值推送通知,

{"aps": {"alert":"content test","badge":1,"sound":"default"}} 

,或者你使用localnotification徽章值设置在applicationIconBadgeNumber属性的UILocalNotification,即

notification.applicationIconBadgeNumber=1; 

因此,如果你在后台应用程序,你有不同的通知两个代表席方法,

所以实现该委托方法推送通知本地通知didReceiveLocalNotification:didReceiveRemoteNotification:

所以请在实施前参考Apple Document

+0

查看问题的更新。 – raginggoat