Artem Koltsov
Artem Koltsov

Reputation: 43

IOS 8 push notification check returns wrong UIRemoteNotificationType

I'm trying to check if PushNotifications for my app are enabled.

In AppDelegate.m I do register app for remote notifications, and in Settings on iPhone (iOS 8) Push Notifications for this app are enabled.

I've googled to methods:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

BOOL check = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

As the result, types = UIRemoteNotificationTypeNone and check = NO.

I'm using this code sample to register application for Push Notifications:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    //-- Set Notification
    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
    {
           // iOS 8 Notifications
           [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

           [application registerForRemoteNotifications];
    }
    else
    {
          // iOS < 8 Notifications
          [application registerForRemoteNotificationTypes:
                     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
    }

     //--- your custom code
     return YES;
}

What is the problem here?

Upvotes: 2

Views: 1828

Answers (2)

cire.boroguies
cire.boroguies

Reputation: 1071

I think you've already found a solution but just in case: in iOS 8, enabledRemoteNoficationTypes is deprecated you should use:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
        UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (currentSettings.types == UIUserNotificationTypeNone) {}

Best

Upvotes: 3

EsbenB
EsbenB

Reputation: 3406

Try checking your state with this code:

UIRemoteNotificationType allOnType = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge ;
UIRemoteNotificationType offType = UIRemoteNotificationTypeNone ;

UIRemoteNotificationType currentTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if (currentTypes == allOnType) {
    // all are on
} else if (currentTypes == offType) {
    // all are off
} else {
    // some are on, some are off
}

Edit

Also, try implementing the Push Notification Callbacks:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{
    NSLog(@"Token%@",deviceToken);    
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
    NSLog(@"err:%@",err);
}

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //for iOS8
}

Upvotes: -1

Related Questions