【问题标题】:Add Push Notification Badge to UIButton [closed]将推送通知徽章添加到 UIButton [关闭]
【发布时间】:2014-02-17 14:13:19
【问题描述】:

当有通知发送时,我想在我的应用程序内的 UIButton 中添加一个徽章图标,但我不太确定如何实现这一点。它需要匹配跳板图标上的徽章,并且当在应用中按下 UIButton 时,跳板徽章和 UIButton 上的徽章都需要消失。

更新:

我有一个徽章出现,但是当我打开应用程序时,徽章会在应用程序打开时重置为零,所以我最终看不到按钮上的徽章。我知道徽章有效,因为我通过这样做对其进行了测试:

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

【问题讨论】:

  • 我一直想知道这是否可能,我也需要这样做!
  • 是的,这是可能的。正如@Viruss 回答的那样,您可以使用 UIApplication 的applicationIconBadgeNumber 属性来设置和获取徽章。

标签: ios objective-c uibutton push-notification badge


【解决方案1】:

对于 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.

如果您的应用处于后台,则徽章将显示在应用图标上,如果您使用的推送通知比从有效负载中设置的徽章值,

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

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

 notification.applicationIconBadgeNumber=1;

因此,如果您的应用在后台,您有两个用于不同通知的委托方法,

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

所以请在实施前参考Apple Document

【讨论】:

  • 查看问题的更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多