簡體   English   中英

設備令牌未為ios 9注冊

[英]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=&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];
                }
            }];
        }
    }
}

可能您應該添加[[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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM