2014-11-05 95 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