1

I have a button in my app that triggers registering for notifications. Say this is a UIBarButton.

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Grant" style:UIBarButtonItemStyleDone target:self action:@selector(grantPermission:)];
self.navigationItem.rightBarButtonItem = rightButton;

This is the function the button triggers:

- (void)grantPermission:(id)sender {
    if (SYSTEM_VERSION_LESS_THAN( @"10.0" )) {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];

    } else {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            NSLog(@"Completion handler called");
            if (!error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication] registerForRemoteNotifications];
                });
            } else {
                NSLog( @"Push registration FAILED" );
                NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
                NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
            }
        }];
    }
}

I also added the following delegate methods:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Did Register for Remote Notifications with Device Token (%@)", deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Did Fail to Register for Remote Notifications");
    NSLog(@"%@, %@", error, error.localizedDescription);
}

When I run this code, I get the modal asking for permission. When I press 'accept' I see the Completion handler called log, but nothing after that. Nothing happens, no logs are shown. Nothing. Any ideas as to what I'm doing wrong?

user4992124
  • 1,574
  • 1
  • 17
  • 35
  • Possible duplicate of [why didRegisterForRemoteNotificationsWithDeviceToken is not called](https://stackoverflow.com/questions/4086599/why-didregisterforremotenotificationswithdevicetoken-is-not-called) – mfaani Sep 30 '17 at 22:46
  • Go through all its answers, additionally are you using a device or simulator? Because you won't be able to get a token through simulator! – mfaani Sep 30 '17 at 22:47

1 Answers1

0

You must test that code on a physical device. Simulators don't get notifications token. You will get an error callback.

Also check which version of iOS you are targeting because the callbacks in the app delegate which changed between ios9-10 (possibly 11) so you will have a deprecated method which won't get called (or visa versa depending on which version of iOS you are testing against)

Avba
  • 14,822
  • 20
  • 92
  • 192