1

I'am trying to get the device token so I can send a notification to it but i keep getting the error "enabledRemoteNotificationTypes is not supported in iOS 8.0 and later."

The device is registered as I can turn the notifications off and on in the notification settings on the phone. This is the code i'm using to try and retrieve the token:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
NSLog(@"This is device token%@", deviceToken);
}

im registering the device with this code:

if ([application   respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // iOS 8 Notifications
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];
    NSLog(@"ios 8+: %@");
}
else
{
    // iOS < 8 Notifications
    [application registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |      UIRemoteNotificationTypeSound)];
    NSLog(@"< ios 8+: %@");
}

Ideally i'd like to retrieve the device tokens and send them to a mysql database, have no clue how to do this as i'm a web developer and not too familiar with Objective C

Matthew Foster
  • 167
  • 1
  • 10
  • didRegisterForRemoteNotificationsWithDeviceToken is not an error. It is a valid callback once you register for push notifications deviceToken as nsdata passed with it contains the device token you can extract and then most likely pass to your backend – ambientlight Jan 31 '16 at 09:12
  • My Bad the error is "enabledRemoteNotificationTypes is not supported in iOS 8.0 and later." – Matthew Foster Jan 31 '16 at 09:16
  • how are you registering for remote notifications? – ambientlight Jan 31 '16 at 09:20
  • just added it to the question. – Matthew Foster Jan 31 '16 at 09:23
  • please refer to http://stackoverflow.com/questions/26091875/how-to-update-code-using-enabledremotenotificationtypes-because-it-is-not-suppo – ambientlight Jan 31 '16 at 09:39
  • the problem is that the function 'didRegisterForRemoteNotificationsWithDeviceToken' wont fire – Matthew Foster Jan 31 '16 at 10:02
  • Weird, should fire, but also Please check in project settings whether the push notifications are enabled and whether you have generated push certificates for development/deployment at Apple developer website. – ambientlight Jan 31 '16 at 10:08
  • Also you won't get push notifications on simulator – ambientlight Jan 31 '16 at 10:11
  • im using my iphone 6 Plus, and if i go into notification setting its set to "allow Notifications" even on the nslog is says im registered, but its saying that the function "didRegisterForRemoteNotificationsWithDeviceToken" is not supported in ios 8 or later – Matthew Foster Jan 31 '16 at 10:22

2 Answers2

1

Here is the way you should register for remote notifications:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)])
{
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
    UIRemoteNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
}

Also if you need to send it to your backend, you first take a look here and extract a string token in didRegisterForRemoteNotificationsWithDeviceToken:: Iphone device token - NSData or NSString

Then use AFNetworking or NSURLSession to send it to your backend API. Take a look here for example: Send POST request using NSURLSession

Community
  • 1
  • 1
ambientlight
  • 7,212
  • 3
  • 49
  • 61
  • Okay so deleted the app from the phone, added the new code then run it again through xcode. It prompt me to recieve notifications which i did but in the log it still wont give me my device token. "enabledRemoteNotificationTypes is not supported in iOS 8.0 and later." – Matthew Foster Jan 31 '16 at 09:32
  • you need to extract a token from NSData argument of didRegisterForRemoteNotificationsWithDeviceToken: http://stackoverflow.com/questions/1587407/iphone-device-token-nsdata-or-nsstring Then use this token and call your backend API in http post request (likely application/x-www-form-urlencoded format) – ambientlight Jan 31 '16 at 09:35
  • 1
    didRegisterForRemoteNotificationsWithDeviceToken won't fire – Matthew Foster Jan 31 '16 at 09:36
  • please refer to http://stackoverflow.com/questions/26091875/how-to-update-code-using-enabledremotenotificationtypes-because-it-is-not-suppo – ambientlight Jan 31 '16 at 09:39
  • Yes that code works great, i can register! but I cannot retrieve the device token to the on NSlog to see it. – Matthew Foster Jan 31 '16 at 09:42
  • One way in one liner is to: NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""]; – ambientlight Jan 31 '16 at 09:45
  • the problem is that the function 'didRegisterForRemoteNotificationsWithDeviceToken' wont fire. – Matthew Foster Jan 31 '16 at 10:00
  • found the issue, two cordova push notification plugins where installed for some reason. removed them and worked :) – Matthew Foster Jan 31 '16 at 10:45
  • Didn't know you are using Cordova) remember to specify in the question next time and also take a look what you can do with Angular 2 – ambientlight Jan 31 '16 at 15:26
0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
 {

        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }

return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{
    NSString *myDeviceToken = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    myDeviceToken = [myDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSLog(@"Device Token: %@", myDeviceToken);
}

Use above code to get Device Token, & to get Remote notification make sure your Background mode is ON for Remote notification

SOFe
  • 7,867
  • 4
  • 33
  • 61