简体   繁体   中英

Push Notifications Cannot get Device Token

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

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

- (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM