dannysandler dannysandler - 1 year ago 83
iOS Question

Ask for User Permission to Receive UILocalNotifications in iOS 8

I have set up local notifications in the App Delegate Using this:

- (void)applicationDidEnterBackground:(UIApplication *)application
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:@"Watch the Latest Episode of CCA-TV"];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];

When I run the app and then quit it I receive an error saying:

2014-06-07 11:14:16.663 CCA-TV[735:149070] Attempting to schedule a local notification {fire
date = Saturday, June 7, 2014 at 11:14:21 Pacific Daylight Time, time
zone = America/Los_Angeles (PDT) offset -25200 (Daylight), repeat
interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount,
next fire date = Saturday, June 7, 2014 at 11:14:21 Pacific Daylight
Time, user info = (null)} with an alert but haven't received
permission from the user to display alerts

How can I get the necessary permission to display the alerts?

Answer Source

Since iOS 8 you need to ask user's permission to show notifications from your app, this applies for both remote/push and local notifications. In Swift you can do it like this,

Update for Swift 2.0

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    // Override point for customization after application launch.
    let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
        notificationCategory.identifier = "INVITE_CATEGORY"
        notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default)

        //registerting for the notification.
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[.Sound, .Alert, .Badge], categories: nil))
       //do iOS 7 stuff, which is pretty much nothing for local notifications.
    return true

Objective C syntax is also very similar.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    // Override point for customization after application launch.
    return YES;

To check for currently registered notification types you can use UIApplication class's method,

- (UIUserNotificationSettings *)currentUserNotificationSettings

So if the user has said no to your app then this function should return a setting without any types in it.

I have written a tutorial about this, you could see it here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download