当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - want to know the state of the Notification center whether is in "ON" or "OFF" state in ios5 and ios6

问题描述:

I want to know the state of the Notification center whether is in "ON" or "OFF" state.

As I know every one says to enable that by using

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

BUt i need to get the state either "ON" or "OFF" state.

I searched google results are:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if (types == UIRemoteNotificationTypeNone)

{

}

and

if (launchOptions != nil)

{

NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (dictionary != nil)

{

NSLog(@"Launched from push notification: %@", dictionary);

[self clearNotifications];

}

}

and

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

}

But i did not get result both ios5 and ios6

Please Guide me

Thanks in Advance

网友答案:

There is a combination of 2 things you can do to make sure you always know when the user does not have notifications enabled in a specific way.

You must handle both didRegisterForRemoteNotificationsWithDeviceToken and didFailToRegisterForRemoteNotificationsWithError in your AppDelegate.

Example

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

FYI: This will always trigger in the simulator so you may want to comment it out until you test on a device as it becomes annoying.

分享给朋友:
您可能感兴趣的文章:
随机阅读: