在iOS 8中请求用户权限接收UILocalNotifications [英] Ask for User Permission to Receive UILocalNotifications in iOS 8

查看:124
本文介绍了在iOS 8中请求用户权限接收UILocalNotifications的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程式委托中设定了本地通知。使用此:

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

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

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
date = 2014年6月7日星期六at 11:14:21太平洋夏令时间,时间
zone = America / Los_Angeles(PDT)偏移-25200(夏令时),重复
interval = 0,repeat count = UILocalNotificationInfiniteRepeatCount,
next fire date = Saturday,June 7,2014 at 11:14:21 Pacific Daylight
Time,user info =(null)}

如何获取必要的

推荐答案

自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
}

目标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类的方法, p>

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.

这篇关于在iOS 8中请求用户权限接收UILocalNotifications的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆