I am checking my app compatibility with iOS 8, I am getting following Log in console "Attempting to badge the application icon but haven't received permission from the user to badge the application" . Can anyone please help me to get rid of this warning. And Yes, my app shows Badges on App Icon and TabBar Icon.
Here is What I did in my AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // registering for remote notifications [self registerForRemoteNotification]; return YES; } - (void)registerForRemoteNotification { if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert; UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } } #ifdef __IPHONE_8_0 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [application registerForRemoteNotifications]; } #endif
Apple makes new API for registering notifications and working with badges.
See WWDC 2014 session video: https://developer.apple.com/videos/wwdc/2014/?id=713 , http://asciiwwdc.com/2014/sessions/713 (text version) and https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/registerUserNotificationSettings:
User can change permissions for every UIUserNotificationType (UIUserNotificationTypeBadge, UIUserNotificationTypeSound, UIUserNotificationTypeAlert)
in Settings.
Before changing badge you must check permissions.
Code sample from my AppDelegate:
- (BOOL)checkNotificationType:(UIUserNotificationType)type { UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; return (currentSettings.types & type); } - (void)setApplicationBadgeNumber:(NSInteger)badgeNumber { UIApplication *application = [UIApplication sharedApplication]; if(SYSTEM_VERSION_LESS_THAN(@"8.0")) { application.applicationIconBadgeNumber = badgeNumber; } else { if ([self checkNotificationType:UIUserNotificationTypeBadge]) { NSLog(@"badge number changed to %d", badgeNumber); application.applicationIconBadgeNumber = badgeNumber; } else { NSLog(@"access denied for UIUserNotificationTypeBadge"); } } } #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
The currentUserNotificationSettings method is available in the UI application instance and will give you the most up-to-date user notification preferences.
Working with badge number:
[self setApplicationBadgeNumber:0];
instead of
application.applicationIconBadgeNumber = 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With