如何检查是否启用了本地通知?

时间:2014-04-04 06:00:21

标签: ios notifications

我知道我们可以使用以下代码检查用户是否已启用/禁用远程通知:

[[UIApplication sharedApplication] enabledRemoteNotificationTypes]

但是检查本地通知呢?

我找不到本地通知类型的相应属性,我已经确认enabledRemoteNotificationTypes仅用于远程通知。

众所周知,用户可以编辑他们的通知设置,这将影响远程和本地。

2 个答案:

答案 0 :(得分:1)

我不确定,但我认为您无法访问此信息。

您可以检查用户是否为您的应用启用了通知的一种方法是向您发送本地通知,延迟时间为1秒:

UILocalNotification *testNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = @"Test notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

并检查你是否抓住了它:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification) {

    // If you get here, notifications are enabled
}

剩下的就是添加信息(例如localNotification.userInfo),这样您就可以在didReceiveLocalNotification:知道您是否正在处理测试通知,或者是否是“真实”通知。

答案 1 :(得分:0)

- (BOOL) isLocalNotificationsEnable {
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
        UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
        return (grantedSettings.types == UIUserNotificationTypeNone) ? NO : YES;
    }
    return NO;
}

注意:仅当您定位<时才需要if块。 iOS 8.0。

相关问题