请求用户许可在 iOS 8 中接收 UILocalNotifications [英] Ask for User Permission to Receive UILocalNotifications in iOS 8
问题描述
我已经在 App Delegate 中设置了本地通知:
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] 尝试安排本地通知 {fire日期 = 2014 年 6 月 7 日星期六 11:14:21 太平洋夏令时间,时间zone = America/Los_Angeles (PDT) 偏移 -25200(日光),重复间隔 = 0,重复计数 = UILocalNotificationInfiniteRepeatCount,下一个开火日期 = 2014 年 6 月 7 日星期六 11:14:21 Pacific Daylight时间,用户信息 = (null)} 有警报但没有收到来自用户的显示警报的权限
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?
推荐答案
从 iOS 8 开始,您需要征得用户的许可才能显示来自您的应用的通知,这适用于远程/推送和本地通知.在 Swift 中你可以这样做,
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,
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 语法也非常相似.
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;
}
要检查当前注册的通知类型,您可以使用 UIApplication 类的方法,
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.
我写了一篇关于这个的教程,你可以看看 此处.
这篇关于请求用户许可在 iOS 8 中接收 UILocalNotifications的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!