Search code examples
iospush-notificationuinavigationitem

Update navigationItem.rightBarButtonItem in MasterViewController on didReceiveRemoteNotification


I have a rightBarButtonItem on my MasterViewController that displays a badge when the app has received a push notification and gets opened via the app icon on the home screen. I am having trouble getting the badge to appear on the button when a notification is received with the app open.

AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    NSNumber *badgeNum = [apsInfo objectForKey:@"badge"];
    MasterViewController *masterView = [[MasterViewController alloc] init];
    [masterView updateNotificationButton:badgeNum];
}

MasterViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *barButtonImg = [UIImage imageNamed:@"ico_notification"];
    UIButton *notificationButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    [notificationButton addTarget:self action:@selector(dealsButton:) forControlEvents:UIControlEventTouchUpInside];
    [notificationButton setImage:barButtonImg forState:UIControlStateNormal];
    BBBadgeBarButtonItem *barButton = [[BBBadgeBarButtonItem alloc] initWithCustomUIButton:notificationButton];
    barButton.badgeOriginX = 13;
    barButton.badgeOriginY = -9;
    NSInteger badgeNum = [UIApplication sharedApplication].applicationIconBadgeNumber;
    barButton.badgeValue = [NSString stringWithFormat:@"%ld", badgeNum];
    self.navigationItem.rightBarButtonItem = barButton;
}

- (void)updateNotificationButton:(NSNumber *)badgeNum {
    BBBadgeBarButtonItem *barButton = (BBBadgeBarButtonItem *)self.navigationItem.rightBarButtonItem;
    barButton.badgeValue = [NSString stringWithFormat:@"%ld", (long)badgeNum];
}

I can see that barButton is nil when updateNotificationButton: is run but I'm not sure what I'm doing wrong.


Solution

  • Use notifications...

        - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
        {
            NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
            NSNumber *badgeNum = [apsInfo objectForKey:@"badge"];
            NSDictionary *userInfo = [NSDictionary dictionaryWithObject:badgeNum forKey:@"badge"];
           [[NSNotificationCenter defaultCenter] postNotificationName:@"badgeUpdationNotification" object:nil userInfo:userInfo];
        }
    

    Then in your MasterViewController viewDidLoad method observe the notification

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UIImage *barButtonImg = [UIImage imageNamed:@"ico_notification"];
        UIButton *notificationButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
        [notificationButton addTarget:self action:@selector(dealsButton:) forControlEvents:UIControlEventTouchUpInside];
        [notificationButton setImage:barButtonImg forState:UIControlStateNormal];
        BBBadgeBarButtonItem *barButton = [[BBBadgeBarButtonItem alloc] initWithCustomUIButton:notificationButton];
        barButton.badgeOriginX = 13;
        barButton.badgeOriginY = -9;
        NSInteger badgeNum = [UIApplication sharedApplication].applicationIconBadgeNumber;
        barButton.badgeValue = [NSString stringWithFormat:@"%ld", badgeNum];
        self.navigationItem.rightBarButtonItem = barButton;
       //Observe notification here
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateNotificationButton:) name:@"badgeUpdationNotification" object:nil];
    }
    
    - (void)updateNotificationButton:(NSNotification *)notification
    {
        int badgeNum = [[[notification userInfo] valueForKey:@"badge"] intValue];
        BBBadgeBarButtonItem *barButton = (BBBadgeBarButtonItem *)self.navigationItem.rightBarButtonItem;
        barButton.badgeValue = [NSString stringWithFormat:@"%d", (long)badgeNum];
    }