13

I want to show an alert when the local notification is fired, but for that I have to ask for permission, as it says to me when I run the app on my iPhone:

Attempting to schedule a local notification {fire date = Friday 13 June 2014 12 h 10 min 27 s Central European Summer Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Friday 13 June 2014 12 h 10 min 27 s Central European Summer Time, user info = (null)} with an alert but haven't received permission from the user to display alerts

How can I do that? Here´s the code as it is now:

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:timeUntilNotification];
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.alertBody = @"ZEIT!";
    localNotif.alertAction = @"Show me the Timer!";
    localNotif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] +1;


    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
user3737316
  • 131
  • 1
  • 1
  • 3

5 Answers5

40

add this code, it will show a alert view to ask user for permission.

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge
                                                                                                          categories:nil]];
}

you can add this code in application:didFinishLaunchingWithOptions; method, so that the app will ask your user when they launch the app, or you can add this code when you set Local Notification, it's up to you.

蘇健豪
  • 541
  • 4
  • 13
  • 3
    This snippet is great, thank you! Note to everyone: put good thought into when specifically in your app you want to ask the user permission to send notifications, because you don't want to scare them off right when they open your app. Build some trust first, then ask their permission. UX food for thought :) – LargeGlasses Oct 31 '14 at 19:01
  • 1
    In swift it looks like this: let registerUserNotificationSettings = UIApplication.instancesRespondToSelector("registerUserNotificationSettings:") if registerUserNotificationSettings { var types: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: nil))} – Morkrom Nov 29 '14 at 03:23
  • This snippet is recommended for iOS 8 and above. – samthui7 Jul 31 '15 at 10:29
4

蘇健豪's answer is good.

In Swift it looks like this:

let registerUserNotificationSettings = UIApplication.instancesRespondToSelector("registerUserNotificationSettings:")

if registerUserNotificationSettings { 
    var types: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound     
    UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotific‌​ationSettings(forTypes: types, categories: nil))
} 

Also see here: Ask for User Permission to Receive UILocalNotifications in iOS 8

Community
  • 1
  • 1
Morkrom
  • 578
  • 7
  • 26
  • 1
    Can i do a re-ask? If user accidentally don't allow the permission, can i make a button and if user tap on it the app will re-ask for the notification permission? – TomSawyer May 14 '16 at 21:59
1
//register notifications
if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) //ios 8+
{
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    [application registerForRemoteNotifications];
}
else // ios 7 or less
{
    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge];
}
Cayke Prudente
  • 220
  • 3
  • 5
1

In the swift language....

var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound;
var setting = UIUserNotificationSettings(forTypes: type, categories: nil);
UIApplication.sharedApplication().registerUserNotificationSettings(setting);
UIApplication.sharedApplication().registerForRemoteNotifications();
josliber
  • 43,891
  • 12
  • 98
  • 133
0

Try this for Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        NSLog(@"didFinishLaunchingWithOptions");

        if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
        }

        return YES; 
    }
Jaime
  • 471
  • 5
  • 6