推送通知无法获取设备令牌

时间:2016-01-31 09:09:57

标签: ios push-notification

我正在尝试获取设备令牌,因此我可以向其发送通知,但我一直收到错误" iOS 8.0及更高版本不支持enabledRemoteNotificationTypes。"

设备已注册,因为我可以在手机的通知设置中关闭和打开通知。这是我用来尝试检索令牌的代码:

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

使用此代码注册设备:

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+: %@");
}

理想情况下,我想检索设备令牌并将它们发送到mysql数据库,不知道如何做到这一点,因为我是一名Web开发人员而不太熟悉Objective C

2 个答案:

答案 0 :(得分:1)

以下是注册远程通知的方式:

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

此外,如果您需要将其发送到后端,请先查看此处并在didRegisterForRemoteNotificationsWithDeviceToken:中提取字符串令牌:Iphone device token - NSData or NSString

然后使用AFNetworking或NSURLSession将其发送到您的后端API。 看看这里例如:Send POST request using NSURLSession

答案 1 :(得分: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);
}

使用上面的代码获取设备令牌,&amp;获取远程通知确保您的后台模式为远程通知