我已在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] 尝试安排本地通知 {fire 日期= 2014年6月7日星期六太平洋夏令时间11:14:21时间 zone = America / Los_Angeles(PDT)偏移-25200(日光),重复 interval = 0,repeat count = UILocalNotificationInfiniteRepeatCount, 下次开火日期= 2014年6月7日星期六太平洋日光11:14:21 时间,用户信息=(null)} 带有提醒但未收到提示 用户允许显示提醒
如何获得显示警报的必要权限?
答案 0 :(得分:237)
从iOS 8开始,您需要询问用户显示来自您应用的通知的权限,这适用于远程/推送和本地通知。在Swift中你可以这样做,
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;
}
要检查当前已注册的通知类型,您可以使用UIApplication类的方法
- (UIUserNotificationSettings *)currentUserNotificationSettings
因此,如果用户对您的应用程序说“不”,则此函数应返回其中没有任何类型的设置。
我写了一篇关于此的教程,你可以看到它here。
答案 1 :(得分:38)
将此代码放在视图控制器中,您将首先对通知进行编程(如果您在启动时对其进行编程,那么它将是application:didFinishLaunchingWithOptions:
):
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
}
在斯威夫特:
if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) {
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Sound, categories: nil))
}
针对系统版本号进行测试的解决方案是次优的且容易出错。
答案 2 :(得分:18)
尝试使用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
}
答案 3 :(得分:5)
我刚遇到同样的问题。似乎在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
系统会记住决定,只会问一次。
答案 4 :(得分:1)
** iOS8 +的三个按钮操作的本地通知
//按钮:我去了,稍后提醒,跳过它**
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()
}
}