[英]Device token is not registered for ios 9
我实现了针对ios的推送通知。但是设备令牌是针对ipad,ios v 8.3获取的,但是当我在iphone 6s plus v9.0中安装应用程序时,设备令牌未注册。我根据此参考创建了所有证书,但我在ipad上收到通知,但没有收到iphone。 那是什么问题,我找不到,请帮帮我
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=®id=%@&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];
}
}];
}
}
}
可能您应该添加[[UIApplication sharedApplication] registerForRemoteNotifications];
在if(IS_OS_8_OR_LATER)
语句中。 希望对你有帮助
以下代码可能会从iOS 6及更高版本中解决您的问题。 从`iOS 8.0不推荐使用registerForRemoteNotificationTypes
。
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.