要求用户权限在iOS 8中接收UILocalNotifications

我已经在App Delegate中设置了本地通知使用这个:

- (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]]; } 

当我运行应用程序,然后退出它,我收到一个错误说:

2014-06-07 11:14:16.663 CCA-TV [735:149070] 试图安排本地通知 {火灾date= 2014年6月7日星期六11:14:21太平洋夏令时,时区= America / Los_Angeles (PDT)偏移-25200(日光),重复间隔= 0,重复计数= UILocalNotificationInfiniteRepeatCount,下一个火灾date= 2014年6月7日星期六11:14:21 Pacific Daylight Time,user info =(null)} 但是还没有得到用户的许可来显示警报

我如何获得显示警报的必要权限?

从iOS 8开始,您需要询问用户的权限以显示来自应用程序的通知,这适用于远程/推送和本地通知。 在斯威夫特你可以这样做,

更新Swift 2.0

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { // Override point for customization after application launch. if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) { 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)) } else { //do iOS 7 stuff, which is pretty much nothing for local notifications. } return true } 

Swift 3.2

 if(UIApplication.instancesRespond(to: #selector(UIApplication.registerUserNotificationSettings(_:)))){ let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory() notificationCategory.identifier = "INVITE_CATEGORY" notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default) //registerting for the notification. application.registerUserNotificationSettings(UIUserNotificationSettings(types:[.sound, .alert, .badge], categories: nil)) } else{ //do iOS 7 stuff, which is pretty much nothing for local notifications. } 

Objective C语法也非常相似。

 - (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; } 

要检查当前注册的通知types,可以使用UIApplication类的方法,

 - (UIUserNotificationSettings *)currentUserNotificationSettings 

所以如果用户对你的应用程序说不,那么这个函数应该返回一个没有任何types的设置。

我已经写了一个关于这个的教程,你可以在这里看到它。

把这段代码放在视图控制器中,你将首先编写通知(如果你在启动时对它们进行编程,那么它将是application:didFinishLaunchingWithOptions:

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

在Swift中:

 if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) { UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Sound, categories: nil)) } 

testing系统版本号的解决scheme是次优的,容易出错。

尝试这个Objective-C:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { // are you running on iOS8? if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:settings]; } else // iOS 7 or earlier { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } } 

对于Swift:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { // Override point for customization after application launch. if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) { application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil)) } else { // } return true } 

我只是面临同样的问题。 看起来像在iOS 8我们需要做一个额外的步骤,通常在里面完成:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /*...*/ } 

如果你想保持向后兼容,你可以使用这个代码:

 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]]; } #endif 

系统会记住这个决定,只会问一次。

**本地通知与三个button的动作为iOS8 +

/ /button:我find它,提醒它,跳过它**

  let completeAction = UIMutableUserNotificationAction() completeAction.identifier = "COMPLETE_TODO" completeAction.title = "I TOOK IT" completeAction.activationMode = .Background completeAction.destructive = true completeAction.authenticationRequired = false let remindAction = UIMutableUserNotificationAction() remindAction.identifier = "REMIND_TODO" remindAction.title = "REMIND LATER" remindAction.activationMode = .Background remindAction.destructive = false // remindAction.authenticationRequired = false let skipAction = UIMutableUserNotificationAction() skipAction.identifier = "SKIP_TODO" skipAction.title = "SKIP IT" skipAction.activationMode = .Background skipAction.destructive = false skipAction.authenticationRequired = false let todoCategory = UIMutableUserNotificationCategory() todoCategory.identifier = "TODO_CATEGORY" todoCategory.setActions([completeAction, remindAction, skipAction], forContext: .Default) todoCategory.setActions([completeAction,remindAction,skipAction], forContext: .Minimal) if application.respondsToSelector("isRegisteredForRemoteNotifications") { let categories = NSSet(array: [todoCategory,todoVideoCategory]) let types:UIUserNotificationType = ([.Alert, .Sound, .Badge]) let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as? Set<UIUserNotificationCategory>) application.registerUserNotificationSettings(settings) application.registerForRemoteNotifications() } }