How to detect if a user clicks the Do Not Allow button in the Apple Push Notification confirmation notification

I want to trigger an event if the user clicks the "Do not allow" button in the notification message when the apple button is clicked. Is there a notice of termination or any other way to detect this action from the user?

+3
source share
3 answers

I am sure that someone will need a solid and simple answer to this question (as I once did) - so here you are. Immediately after the call, [[UIApplication sharedApplication] registerForRemoteNotifications];you can use NSNotificationCenterit beautifully like this:

[[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"
                                                  }
                                              }];

. iOS 9.2, Xcode - 7.2, - 8.0.

+14

, , UIAlertView, iOS - ..

Don't Allow, push- iOS, YES.

.

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
   // NONE
+1
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.

= > / .

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

    }

=> Here below the method is called After Alert Notification Fire. Using any button action (do not allow or allow), we forcefully register the notification completely. and Dow with some fixes.

#ifdef __IPHONE_8_0

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

=> We do the 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
    }
}
0
source

All Articles