简体   繁体   中英

How to set badge for UIBarButtonItem from Appdelegate (whenever push notification comes) if UITabbarcontroller is not the rootviewcontroller?

How to set badge for UIBarButtonItem from Appdelegate (whenever push notification comes) if UITabbarcontroller is not the rootviewcontroller ?

I have a LoginViewController and a PinViewController before UITabbarcontroller. I'm setting LoginViewController as rootviewcontroller if user has not logged in and PinViewController as rootviewcontroller if user has already logged in. But I found that we could set badge for UIBarButtonItem from Appdelegate only if the rootviewcontroller is UITabbarcontroller.

Can anyone help me with this?

You can manage it by Posting Notifications

// In App Delegate Class

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

    NSNumber *count = [NSNumber numberWithInt:5]; // This is static...

    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:count
                                                         forKey:@"count"];

    application.applicationIconBadgeNumber = 0;

    if (application.applicationState == UIApplicationStateActive)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"getPushNotification" object:dataDict];
    }
}

In Controller where you want to update badge count on bar.

- (void)viewDidLoad {
    [super viewDidLoad];



    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateBadgeCount:)
                                                 name:@"getPushNotification"
                                               object:nil];

}


- (void)updateBadgeCount:(NSNotification *)note {
    NSDictionary *theData = [note userInfo];
    if (theData != nil) {
        NSNumber *n = [theData objectForKey:@"count"];


        // Do your stuff here...
    }
}

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM