sanjana
sanjana

Reputation: 3034

How to detect if user tap on "Don't Allow" on Apple's push notification confirmation alert

I want to fire some event if user taps on "Don't Allow" button on the apple's push notification alert message. Is there any notification getting fired or any other way to detect this action from the user?

Upvotes: 3

Views: 3680

Answers (3)

LargeGlasses
LargeGlasses

Reputation: 912

I'm sure someone will need a solid and simple answer to this (like I once did) -- so here you go. Directly after calling [[UIApplication sharedApplication] registerForRemoteNotifications]; you can use NSNotificationCenter beautifully like so:

[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification
                                                  object:nil
                                                   queue:[NSOperationQueue mainQueue]
                                              usingBlock:^(NSNotification * _Nonnull note) {
                                                  if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
                                                      //user tapped "Allow"
                                                  }
                                                  else{
                                                      //user tapped "Don't Allow"
                                                  }
                                              }];

NOTE: My device is currently running iOS 9.2, my Xcode is version 7.2, and my Deployment Target is 8.0.

Upvotes: 14

Renish Dadhaniya
Renish Dadhaniya

Reputation: 10752

iOS8 comes with rregisterUserNotificationSettings: delegate method. Using this method we can do some patches.Please review them and Let us know your comments.Please these is only work with iOS8.

=> Add/Register Notification.

   if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    }

=> Here Below method is called After Alert Notification Fire. Using Any of the Button Action(Don't allow or Allow) we force fully register Notification. and Dow with some patch Here.

#ifdef __IPHONE_8_0

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}

=>We do some trick here

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

    NSLog(@"devToken: %@",devToken);

#if !TARGET_IPHONE_SIMULATOR
    NSString* deviceToken = [[[[[devToken description]
                                stringByReplacingOccurrencesOfString: @"<" withString: @""]
                               stringByReplacingOccurrencesOfString: @">" withString: @""]
                              stringByReplacingOccurrencesOfString: @" " withString: @""] retain];

    NSLog(@"deviceToken : %@",deviceToken);

    NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];

    if((![[standardDefaults valueForKey:@"DeviceToken"] isEqualToString:deviceToken]) || [standardDefaults valueForKey:@"DeviceToken"]==nil){
        [self sendProviderDeviceToken:deviceToken];
    }else{
        //Do Some Stuff Here
    }
}

Upvotes: 0

Buntylm
Buntylm

Reputation: 7373

I do't thing so that we can detect what UIAlertView button user pressed as there is no any kind of callback methods or delegate etc provided in iOS.

Only if you pressed Don't Allow this will disable the push notification service for that particular iOS App and if YES then enable.

And After that through the code we can check and ensure about it using.

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
   // NONE

Upvotes: 1

Related Questions