首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设备令牌未为ios 9注册

设备令牌未为ios 9注册
EN

Stack Overflow用户
提问于 2016-01-13 15:13:42
回答 2查看 786关注 0票数 0

我实现了ios.But的推送通知,设备令牌为ipad,ios v8.3,但当我在iphone6s plus v9.0中安装应用程序时,deveice令牌未注册。我根据this reference创建所有证书,但我在ipad中收到通知,但没有获得iphone。那有什么问题呢?我找不到out.Please帮我

代码语言:javascript
复制
if(IS_OS_8_OR_LATER) {
    //Right, that is the point
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                         |UIRemoteNotificationTypeSound
                                                                                         |UIRemoteNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
else{
    //register to receive notifications
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}


- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [application registerForRemoteNotifications];
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token=[deviceToken description];
    token=[token stringByReplacingOccurrencesOfString:@">" withString:@""];
    token=[token stringByReplacingOccurrencesOfString:@"<" withString:@""];
    token=[token stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (token != nil) {
        [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DEVICE_TOKEN"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    NSLog(@"token-->%@",token);
}

-(void)registerDeviceToken
{
    NSUserDefaults* errDefaults = [NSUserDefaults standardUserDefaults];
    NSString *regerr = @"no valid 'aps-environment' entitlement string found for application";
    if ([[errDefaults objectForKey:@"ErrDesc"]  isEqual: regerr]  || [errDefaults objectForKey:@"DEVICE_TOKEN"] == nil) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Device token not register." message:@"Please check in member center that you have valid provisioning profile for your app." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    else{
        NSUserDefaults* statusvalue = [NSUserDefaults standardUserDefaults];
        NSString *status = [statusvalue objectForKey:@"status"];
        NSLog(@"Status->%@",status);
        if ([status  isEqualToString: @"SUCCESS"])
        {
            NSLog(@"%@",@"No need to register!!!");
        }
        else
        {
            NSString *urlString=[NSString stringWithFormat:@"%@?email=&regid=%@&app_type=utnews_v1&mobile=%@",webAPIURL,[[NSUserDefaults standardUserDefaults] objectForKey:@"DEVICE_TOKEN"],[[NSUserDefaults standardUserDefaults] objectForKey:@"mobile_no"]];

            NSURL *url=[NSURL URLWithString:urlString];
            NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
            NSLog(@"url-->%@",request.URL);
            [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                if(error)
                {
                    [[[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
                }
                else if (data.length>0)
                {
                    NSString *responseString=[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
                    NSLog(@"Response string-->%@",responseString);
                    NSMutableDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                    NSString *parsevalue = parsedObject[@"status"];

                    NSLog(@"%@",parsevalue);

                    NSUserDefaults* statusvalue = [NSUserDefaults standardUserDefaults];

                    // Store data in prefereances
                    [statusvalue setObject:parsevalue forKey:@"status"];

                    //  Save to disk
                    [statusvalue synchronize];
                    // NSDictionary *jsonDict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                }
                else
                {
                    // [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"No response from server.Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
                }
            }];
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-01-13 17:09:02

也许你应该在if(IS_OS_8_OR_LATER)语句中添加[[UIApplication sharedApplication] registerForRemoteNotifications];。希望它能帮上忙

票数 1
EN

Stack Overflow用户

发布于 2016-01-13 17:27:09

下面的代码可能会在iOS 6或更高版本中解决您的问题。registerForRemoteNotificationTypes已从` `iOS 8.0中弃用。

代码语言:javascript
复制
#ifdef __IPHONE_8_0
if (IS_OS_EQUAL_GRETER_8) {
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    UIUserNotificationSettings *notificationSetting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound| UIRemoteNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSetting];
}else{
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound| UIRemoteNotificationTypeBadge)];
} 
#else

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:   (UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound| UIRemoteNotificationTypeBadge)];


#endif
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34760494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档