0

EDIT

I'll put this at the top as this seems to possibly be the issue. After registering user settings, I NSLog:

NSLog(@"current notifications : %@", [application currentUserNotificationSettings]);

I receive back:

current notifications : <UIUserNotificationSettings: 0x165844a0; types: (none);>

But as you can see, I am clearly registering the settings to use badge, sound, and alerts.

Also, I guess I should mention that I have turned off notifications in my settings. However, I expect it to call didFailToRegisterForRemoteNotificationsWithError in this case which is where I generate my own phone ID.

-- I take back what I said above. When I delete the app and reinstall it, it turns back on Allow Notifications. --


I know this question is on here a lot, but I just cannot seem to get this working. Here is my dumbed-down code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [self registerForNotifications:application];
}

- (void)registerForNotifications:(UIApplication *)application {
    // Let the device know we want to receive push notifications
    NSLog(@"Registering for Remote Notifications");

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        #ifdef __IPHONE_8_0
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                                 |UIRemoteNotificationTypeSound
                                                                                                 |UIRemoteNotificationTypeAlert) categories:nil];
            [application registerUserNotificationSettings:settings];
        #endif
    } else {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }
    NSLog(@"Done registering for Remote Notifications");
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    NSLog(@"Did register for remote notification settings");
    //register to receive notifications
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    NSLog(@"There was an error?");
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){

    }else if ([identifier isEqualToString:@"answerAction"]){

    }
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@\nMy  device token is: %@\nSend Phone ID is: %@", [self loadSettings:@"phone_id"], deviceToken, [self loadSettings:@"send_phone_id"]);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Failed to register for remote notifications");
}

For my NSLog statements, this is all I receive:

2014-11-11 14:57:11.066 eTicket[3144:988203] Registering for Remote Notifications
2014-11-11 14:57:11.083 eTicket[3144:988203] Done registering for Remote Notifications
2014-11-11 14:57:11.148 eTicket[3144:988203] Settings Validated?: NO
2014-11-11 14:57:11.219 eTicket[3144:988203] Did register for remote notification settings

Ignore the "Settings Validated" line. That's just me checking to see if they've successfully logged in already to skip some steps. That part works and doesn't have anything to do with remote notifications.

I'm getting no indication that it registered for notifications successfully or not, or that there is an error. The application does not crash or throw errors. I am lost as to why none of the bottom methods are being hit.

Thanks, James

James
  • 3,725
  • 4
  • 44
  • 77
  • In my experience `didRegisterForRemoteNotificationsWithDeviceToken` only gets called the first time you register for notifications. You'll probably need to delete the app and re-install it from the simulator or device. – brandonscript Nov 11 '14 at 20:05
  • @remus I have uninstalled the app every time before re-building. Since it was installed previously, it never prompts me to confirm it again, but this has always been called before if they ever accepted yes, or the failed to is always called if they selected no. At least, prior to IOS 8. – James Nov 11 '14 at 20:07
  • Want to try [this](http://stackoverflow.com/questions/9959629/how-to-clear-preference-ios-push-notifications-for-my-application) just in case? – brandonscript Nov 11 '14 at 20:12
  • @remus I set it out for one day from now with the same results, then 3 days from now and still got the same results. Thanks anyway though. – James Nov 12 '14 at 14:14

1 Answers1

0

Well it seems that I had everything right for the most part above. However, after registering the settings, I needed to check to see if any settings were actually turned on. If not, I needed to send them to my own function. So I did this:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    NSLog(@"Did register for remote notification settings");
    NSLog(@"current notifications : %@", [[UIApplication sharedApplication] currentUserNotificationSettings]);
    //register to receive notifications
    if (notificationSettings.types) {
        NSLog(@"user allowed notifications");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }else{
        NSLog(@"user did not allow notifications");
        // show alert here
        [self doNotRegisterForRemoteNotifications];
    }
}

doNotRegisterForRemoteNotifications is my own function where I do what I need to do to generate a phone ID of my own.

James
  • 3,725
  • 4
  • 44
  • 77