检查用户是否允许本地通知. iOS 8. Obj-C

如果用户允许本地通知,是否没有简单的测试方法?在我拒绝发送本地通知后,我在控制台中发现了警告.当相关事件发生时,它表示该应用程序尝试发送通知,即使用户不允许它.我想在尝试显示通知之前检查是否允许它.在我的情况下看评论,我该怎么做?

我的代码是:

app = [UIApplication sharedApplication];
-(void)showBackgroundNotification:(Nsstring *) message {
//check if app is in background and check if local notifications are allowed.
    if (app.applicationState == UIApplicationStateBackground /*&& localnotificationsAreAllowed*/){
        UIlocalnotification *note = [[UIlocalnotification alloc]init];
        note.alertBody = message;
        note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
        [app schedulelocalnotification :note];
    }
}

我得到用户提示,如下所示:

UIUserNotificationSettings *settings;
if ([app     respondsToSelector:@selector(registerUserNotificationSettings:)])
{
    settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotification    TypeSound) categories:nil];
    [app registerUserNotificationSettings:settings];
}

我不能使用设置对象?

编辑:我想我已经解决了.这似乎有效.

-(void)showBackgroundNotification:(Nsstring *) message {
    if (app.applicationState == UIApplicationStateBackground && [app currentUserNotificationSettings].types != UIUserNotificationTypeNone){
        UIlocalnotification *note = [[UIlocalnotification alloc]init];
        note.alertBody = message;
        note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
        [app schedulelocalnotification :note];
    }
}

解决方法

这是我在不太具体的情况下使用的内容
+ (BOOL)notificationsEnabled {
    UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    return settings.types != UIUserNotificationTypeNone;
}

我通常在通知管理器中保留一组这些类型的方法.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...