Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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;