2014-11-05 64 views
0

目前我的应用程序收到有关RSS提要更新的通知。如果用户从通知中打开应用程序,它将打开到正确的视图控制器。如果用户不确认通知并从应用程序图标打开应用程序,则当用户在应用程序内打开菜单时,该rss供稿的表格视图单元格带有带有applicationIconBadgeNumber的徽章图标。选中该行后,该单元格上的徽章将消失,并重置applicationIconBadgeNumber。我的问题是关于想要发送有关应用内其他信息的通知,如会员优惠。我如何区分表视图中的哪一行获得徽章?假设用户收到关于会员福利的通知。我希望徽章出现在表格视图的成员权益行中,但是如果有来自RSS提要的通知,请标记适当的行。UITableView Cell中的通知徽章

这是我目前如何为RSS提要行添加徽章。

在didSelectRowAtIndexPath方法
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; 
      } 
     } 
    } 

中的cellForRowAtIndexPath

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串空值。

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

您可以根据单元格的属性来选择,例如if(cell.title == @“RSS”){do this}。 – 2014-11-05 15:56:59

+0

@FawadMasud,我知道。问题是确定通知的内容,以便正确的单元格被标记。 – raginggoat 2014-11-05 16:12:59

回答

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:方法。

+0

请参阅我的更新。 – raginggoat 2014-11-06 14:28:40

+0

要更新表视图类中的变量“notificationType”,您可以使用委托方法,也可以将其存储在可供所有类访问的位置,例如用户默认值。 – 2014-11-06 14:47:10

+0

我尝试使用用户默认值,但它仍然无法正常工作。这就像我在didReceiveRemoteNotification中做错了什么:但我不确定是什么。 – raginggoat 2014-11-06 18:32:46

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"]) { 
     .... 
    } 

在附注上,我建议尽量避免字符串比较,并使用类似枚举的东西。

+0

这正是我所做的。 – raginggoat 2014-11-06 15:57:37

+0

您确定要保存appDelegate中的notificationType吗?因为你的applicationDidReceiveRemoteNotification中的代码似乎并不意味着这一点。 (notificationtype =而不是self.notificationType)。 – 2014-11-07 09:33:52

+0

我已将其更改为self.notificationType,但它仍然无效。 – raginggoat 2014-11-07 14:05:00