YogevSitton
YogevSitton

Reputation: 10108

iOS push notifications permission prompt - what did the user select?

Is the a way of knowing if the user declined the push notifications permissions?

I know that didRegisterForRemoteNotificationsWithDeviceToken will be called in case the user allowed push notifications - but what will be called if he didn't?

Upvotes: 0

Views: 153

Answers (3)

Vizllx
Vizllx

Reputation: 9246

A simple method to check whether notification is enabled in app or not.

-(BOOL)checkNotificationEnabled
    {

        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {

        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
            if (types == UIRemoteNotificationTypeNone)
            {
                return FALSE; //Notification not enabled
            }
            else
            {
                return TRUE;//Notification is  enabled

            }

        }
        else // for iOS 8 devices checking will be different
        {
            return [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
            // if return TRUE then Notification is  enabled
            // if return False then Notification is  not enabled

        }

    }

Upvotes: 1

iPhone developer.
iPhone developer.

Reputation: 2132

You can implement in following way. Suppose that if user allows notification then you can store device token into user default. Now, next time you can check with same user default weather user allowed or not.

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{

    NSString *aStrToken = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    aStrToken = [aStrToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *aStrStoredToken = [_appDelegate validateString:[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceID"] :@""];

    if([aStrStoredToken length]==0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"getDeviceToken" object:nil];

        [self setApplicationBadgeNumber:0];
        [[NSUserDefaults standardUserDefaults]setObject:aStrToken forKey:@"deviceID"];
        [[NSUserDefaults standardUserDefaults]synchronize];

        if(![[NSUserDefaults standardUserDefaults]objectForKey:@"setDeviceID"]) {
            [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"setDeviceID"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        } else {
            [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"setDeviceID"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        }
    }
}

Upvotes: 0

ndmeiri
ndmeiri

Reputation: 5029

According to the UIApplicationDelegate Protocol Reference, application(_:didFailToRegisterForRemoteNotificationsWithError:) is called if an error occurs during the registration process.

After you call the registerForRemoteNotifications method of the UIApplication object, the app calls this method when there is an error in the registration process.

For more information about how to implement remote notifications in your app, see Local and Remote Notification Programming Guide.

Upvotes: 0

Related Questions