Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug output mentions I should ask for Application Badge permission

I made a very simple app that can run in the background while a timer runs. If the app is still in the background and the timer ends it will send a local notification and set the application badge to 1. When I start the app I always clear it. I noticed that after installing Xcode 6 I was getting this message every time I started the app:

"Attempting to badge the application icon but haven't received permission from the user to badge the application"

Clearly that text is generated by my app setting the badge to 0 to clear it. Where do I set these permissions or request them? Is it now regarded as a push notification?


The problem has been fixed and the answer is posted below. The bottom line is that you need to get confirmation from the user for any kind of notification, while that used to be true only for push notifications.

like image 934
Lucas van Dongen Avatar asked Jun 04 '14 02:06

Lucas van Dongen


1 Answers

I ended up not using the Application Badge at all and I abandoned the initial code snippet that I have posted here in the mean while. Since there are still people reading and commenting on this question I will add my working current solution here as well. It does contains checks for iOS7 but I do not use the callback method. Also this version doesn't just ask for Application Badge permission anymore.

Solution

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

This is what I use now

.h file

#import <Foundation/Foundation.h>

@interface NotificationPermissionHandler : NSObject

+ (void)checkPermissions;
+ (bool)canSendNotifications;

@end

.m file:

#import "NotificationPermissionHandler.h"

@implementation NotificationPermissionHandler

static const UIUserNotificationType USER_NOTIFICATION_TYPES_REQUIRED = UIUserNotificationTypeAlert | UIUserNotificationTypeSound;
static const UIRemoteNotificationType REMOTE_NOTIFICATION_TYPES_REQUIRED = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;

+ (void)checkPermissions;
{
    bool isIOS8OrGreater = [[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)];
    if (!isIOS8OrGreater)
    {
        [NotificationPermissionHandler iOS7AndBelowPermissions];
        return;
    }

    [NotificationPermissionHandler iOS8AndAbovePermissions];
}

+ (void)iOS7AndBelowPermissions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:REMOTE_NOTIFICATION_TYPES_REQUIRED];
}

+ (void)iOS8AndAbovePermissions;
{
    if ([NotificationPermissionHandler canSendNotifications])
    {
        return;
    }

    UIUserNotificationSettings* requestedSettings = [UIUserNotificationSettings settingsForTypes:USER_NOTIFICATION_TYPES_REQUIRED categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:requestedSettings];
}

+ (bool)canSendNotifications;
{
    UIApplication *application = [UIApplication sharedApplication];
    bool isIOS8OrGreater = [application respondsToSelector:@selector(currentUserNotificationSettings)];

    if (!isIOS8OrGreater)
    {
        // We actually just don't know if we can, no way to tell programmatically before iOS8
        return true;
    }

    UIUserNotificationSettings* notificationSettings = [application currentUserNotificationSettings];
    bool canSendNotifications = notificationSettings.types == USER_NOTIFICATION_TYPES_REQUIRED;

    return canSendNotifications;
}

@end

This was my first solution

I kept it just as a reference for to the initial discussion. This code is not maintained.

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

You can also stack permissions into one request by doing this:

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

Also since iOS 8 it's possible to figure out what kind of alerts are allowed by the user:

UIUserNotificationSettings* notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (notificationSettings.types == UIUserNotificationTypeBadge)
{
     // change the badge
}

I ended up using this code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults objectForKey:@"first_run"])
    {
        [self setDefaults];
    }

    [self askAlertPermissions];

    if ([self canChangeBadge])
    {
         [self setBadge:0];
    }

    return YES;
}

- (void)setDefaults;
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:[NSNumber numberWithBool:NO] forKey:@"alerts_allowed"];
    [defaults setObject:[NSDate date] forKey:@"first_run"];
    // More defaults if needed

    [defaults synchronize];
}

- (void)askAlertPermissions;
{
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
}

// This will be called only after confirming your settings
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // There is also a built in method to find out if the user has appropriate settings, you might want to use that instead if you just want to know what the setting is
    [defaults setObject:[NSNumber numberWithBool:YES] forKey:@"alerts_allowed"];
}

- (bool)canChangeBadge;
{
    UIUserNotificationSettings* notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    return notificationSettings.types == UIUserNotificationTypeBadge;
}

More to read:

https://developer.apple.com/library/content/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

https://developer.apple.com/documentation/uikit/uiapplication

like image 144
Lucas van Dongen Avatar answered Nov 07 '22 16:11

Lucas van Dongen