ios - 将推送通知角标(Badge)添加到 UIButton

标签 ios objective-c uibutton push-notification badge

<分区>

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

更新:

我要显示一个角标(Badge),但是当我打开应用程序时,角标(Badge)会在应用程序打开时重置为零,所以我最终在按钮上看不到角标(Badge)。我知道角标(Badge)有效,因为我通过这样做对其进行了测试:

[[UIApplication sharedApplication]setApplicationIconBadgeNumber:1];

这是角标(Badge)的代码:

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;
    }

如何才能让角标(Badge)在打开应用时不重置,而是在应用中按下该按钮时重置?

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

- (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;
    }

最佳答案

对于 UIButton 上的角标(Badge),您可以使用一些可用的自定义库 here ,

要设置/获取您的应用程序角标(Badge),您可以使用方法,

[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.

如果您的应用程序在后台,角标(Badge)将显示在应用程序图标上,如果您使用的是推送通知而不是从负载中设置的角标(Badge)值,

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

或者您正在使用在 UILocalNotification 的 applicationIconBadgeNumber 属性中设置的本地通知标志值,即

 notification.applicationIconBadgeNumber=1;

因此,如果您的应用程序在后台运行,您有两种用于不同通知的委托(delegate)方法,

因此实现委托(delegate)方法本地通知 didReceiveLocalNotification:didReceiveRemoteNotification: 用于推送通知。

所以请引用Apple Document实现前。

关于ios - 将推送通知角标(Badge)添加到 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21831325/

相关文章:

ios - CSS3 列和 UIWebView

ios - 如何将图像分成多个部分进行图像处理,然后重新组合?

objective-c - 解析 XML 存储到 NSArray

ios - UIButton 的 .touchUpInside 事件不起作用

ios - 按下按钮时更改图像,然后在 5 秒后让图像返回原始图像

ios - 处理来自锁定屏幕的来电时,iOS VoIP CallKit 应用程序出现问题

ios - UITableView 标题的布局错误地向下移动

iOS:在矩形中的一点绘制文本

ios - 从 Swift 中的 Objective C 项目访问 App 委托(delegate)

ios - 在新 ViewController 中使用代码时发送到实例的 UIButton 无法识别的选择器