Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS iOS Foundations Notifications Post

What is a better way to show badge notifications for Ribbit?

This is my code in the app delegate. It feels very redundant and inefficient although it works, albeit buggy (I have to open and close the app to get the number to update). Is there a way to simply refer to the message array from the inbox view controller to get the count, instead of re-initializing a message array from Parse?

Also, I am still researching how I can get the badge notifications to update while my app is in the background... Any pointers are appreciated!

In the applicationDidEnterBackground method...

'''Objective-C { PFQuery *query = [PFQuery queryWithClassName:@"Messages"]; [query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { self.messages = objects; }];

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = nil;
notification.applicationIconBadgeNumber = (int)self.messages.count;
UIApplication *app = [UIApplication sharedApplication];

[app presentLocalNotificationNow:notification];        

}
'''

1 Answer

So I may have answered my question..

In the inbox VC, I simply put the line below in the retrieveMessages method.

[UIApplication sharedApplication].applicationIconBadgeNumber = self.messages.count;

In the app delegate, I put the line below in the applicationDidBecomeActiveMethod as suggested in SO. This resets the badge Icon when user is not logged in.

application.applicationIconBadgeNumber = 0;