UITableView Cell中的通知徽章

时间:2014-11-05 15:41:22

标签: ios objective-c uitableview apple-push-notifications

目前,我的应用会收到有关RSS Feed更新的通知。如果用户从通知中打开应用程序,则会打开正确的视图控制器。如果用户未确认通知并从应用程序图标打开应用程序,则当用户在应用程序中打开菜单时,该RSS订阅源的表格视图单元格将带有带有applicationIconBadgeNumber的徽章图标。选择该行后,该单元格上的徽章将消失,并重置applicationIconBadgeNumber。我的问题是想要在应用程序中发送有关其他信息的通知,例如会员福利。如何区分表格视图中的哪一行获得徽章?假设用户收到有关会员福利的通知。我希望徽章显示在表视图的成员权益行中,但如果有来自RSS提要的通知,请标记相应的行。

以下是我目前为RSS Feed行添加徽章的方式。

in cellForRowAtIndexPath:

if (!(indexPath.row == 0))
    {
        cell.accessoryView = nil;
    }

    badgeNumber = [NSString stringWithFormat:@"%ld", (long)[[UIApplication sharedApplication]applicationIconBadgeNumber]];

    actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber withStringColor:[UIColor whiteColor] withInsetColor:[UIColor redColor] withBadgeFrame:NO withBadgeFrameColor:[UIColor redColor] withScale:1.0 withShining:NO withShadow:NO];
    actionAlertBadge.frame = CGRectMake(83, 6, 30, 30);

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

    if (actionAlertBadge.hidden == NO)
    {
        if (indexPath.section == 0)
        {
            if (indexPath.row == 0)
            {
                cell.accessoryView = actionAlertBadge;
            }
        }
    }

在didSelectRowAtIndexPath中:

if (indexPath.row == 0)
        {
            ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain];
            WebViewController *wvc = [[WebViewController alloc]init];
            [actionAlerts setWebViewController:wvc];
            [[UAPush shared] resetBadge];
            actionAlertBadge.hidden = YES;
            [tableView reloadData];
            navController = [[KFBNavControllerViewController alloc]initWithRootViewController:actionAlerts];

            [UIView transitionWithView:appDelegate.window
                              duration:0.5
                               options:UIViewAnimationOptionTransitionFlipFromRight
                            animations:^{
                                appDelegate.window.rootViewController = navController;
                            }
                            completion:nil];
        }

编辑:以下是我尝试完成此操作的方法,但由于我的表视图中的notificationType字符串为NULL,因此无效。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    UA_LINFO(@"Received remote notification: %@", userInfo);

    // Send the alert to UA so that it can be handled and tracked as a direct response. This call
    // is required.
    [[UAPush shared]appReceivedRemoteNotification:userInfo applicationState:application.applicationState];

    // Optionally provide a delegate that will be used to handle notifications received while the app is running
    // [UAPush shared].delegate = your custom push delegate class conforming to the UAPushNotificationDelegate protocol

    // Reset the badge after a push received (optional)
    [[UAPush shared] resetBadge];

    NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];

    NSString *alertMsg = @"";

    if ([apsInfo valueForKey:@"alert"] != NULL) {
        alertMsg = [apsInfo valueForKey:@"alert"];

        if ([alertMsg containsString:@"ACTION ALERT"]) {
            notificationType = @"action alert";
        }
        else if ([alertMsg containsString:@"MEMBER BENEFIT"]) {
            notificationType = @"member benefit";
        }
    }
}

的cellForRowAtIndexPath:

KFBAppDelegate *appDelegate = (KFBAppDelegate *)[[UIApplication sharedApplication]delegate];
    NSString *notificationType = appDelegate.notificationType;
    // NSLog(@"notificationType menu table: %@", notificationType);
    badgeNumber = [NSString stringWithFormat:@"%ld", (long)[[UIApplication sharedApplication]applicationIconBadgeNumber]];
    actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber withStringColor:[UIColor whiteColor] withInsetColor:[UIColor redColor] withBadgeFrame:NO withBadgeFrameColor:[UIColor redColor] withScale:1.0 withShining:NO withShadow:NO];
    actionAlertBadge.frame = CGRectMake(83, 6, 30, 30);

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

    if (actionAlertBadge.hidden == NO) {
        if ([notificationType isEqualToString:@"action alert"]) {
            if (indexPath.section == 0) {
                if (indexPath.row == 0) {
                    cell.accessoryView = actionAlertBadge;
                }
            }
        }
        else if ([notificationType isEqualToString:@"member benefit"]) {
            if (indexPath.section == 0) {
                if (indexPath.row == 5) {
                    cell.accessoryView = actionAlertBadge;
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

首先,在发送通知时,您应该在通知中添加类型字段。你会得到像这样的json字典

{"aps":{"alert":{"type":"rss","text":"Hello, world!"},"sound":"default","badge":3}}

获取函数中的类型值(如果应用程序正在运行)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary     *)dict {    
// get the type information out of the dictionary 
// now you can perform depending on this type.
 }

如果应用未运行,则根据apple's documentation

“如果推送通知到达时应用程序未运行,该方法将启动应用程序并在启动选项字典中提供相应的信息。应用程序不会调用此方法来处理推送通知。而是执行应用程序:willFinishLaunchingWithOptions:应用程序:didFinishLaunchingWithOptions:方法需要获取推送通知有效负载数据并做出相应的响应。“

应用程序:didFinishLaunchingWithOptions:方法中获取字典。

答案 1 :(得分:0)

在AppDelegate中,首先制作一个属性:

@property (nonatomic, copy) NSString *notificationType;

然后,在你的

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

使用:

self.notificationType = @"action alert";
self.notificationType = @"member benefit";

并在你的CellForRowAtIndexPath中:

if (actionAlertBadge.hidden == NO) {
    if ([appDelegate.notificationType isEqualToString:@"action alert"]) {
       ....
    }
    else if ([appDelegate.notificationType isEqualToString:@"member benefit"]) {
       ....
    }

在旁注中,我建议尝试避免字符串比较并使用类似枚举的内容。