1

我想知道通知中心的状态是处于“ON”还是“OFF”状态。

据我所知,每个人都说通过使用

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

但是我需要获得“ON”或“OFF”状态。

在此处输入图像描述

我搜索谷歌结果是:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
{
}

if (launchOptions != nil)
    {
        NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {
            NSLog(@"Launched from push notification: %@", dictionary);

            [self clearNotifications];
        }
    }

if (notificationTypes == UIRemoteNotificationTypeNone) {
    // Do what ever you need to here when notifications are disabled
} else if (notificationTypes == UIRemoteNotificationTypeBadge) {
    // Badge only
} else if (notificationTypes == UIRemoteNotificationTypeAlert) {
    // Alert only
} else if (notificationTypes == UIRemoteNotificationTypeSound) {
    // Sound only
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)) {
    // Badge & Alert
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)) {
    // Badge & Sound        
} else if (notificationTypes == (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) {
    // Alert & Sound
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) {
    // Badge, Alert & Sound     
}

但我没有得到 ios5 和 ios6 的结果

请指导我

提前致谢

4

1 回答 1

2

您可以结合两件事来确保您始终知道用户何时没有以特定方式启用通知。

您必须在 AppDelegate 中同时处理 didRegisterForRemoteNotificationsWithDeviceToken 和 didFailToRegisterForRemoteNotificationsWithError。

例子

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    if (!([[UIApplication sharedApplication] enabledRemoteNotificationTypes] & UIRemoteNotificationTypeAlert) || !([[UIApplication sharedApplication] enabledRemoteNotificationTypes] & UIRemoteNotificationTypeBadge)) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Notice" message:@"You do not have the recommended notification settings enabled for this app."   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Notice" message:@"Your device does not currently have notifications enabled."   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

仅供参考:这将始终在模拟器中触发,因此您可能需要将其注释掉,直到您在设备上进行测试,因为它变得烦人。

于 2012-12-01T21:36:35.810 回答