设备总是显示注册推送通知失败



我在应用程序中使用推送通知服务。

但是它现在总是显示(当应用程序启动时)它没有注册推送通知。

我使用了下面的代码推送通知:

谁能告诉我我哪里出错了?

提前感谢。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if(application.applicationIconBadgeNumber != 0){
        [[[[[self tabBarController] viewControllers] objectAtIndex: 1] tabBarItem] setBadgeValue:@"new"];}


     [self.window addSubview:tabBarController.view];
     [self.window makeKeyAndVisible];
     NSLog(@"Registering for push notifications...");    
     [[UIApplication sharedApplication] 
     registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeAlert | 
      UIRemoteNotificationTypeBadge | 
      UIRemoteNotificationTypeSound)];
        return YES;
}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    NSLog(@"asdfasdf");
    NSString *str = [NSString 
                     stringWithFormat:@"Device Token=%@",deviceToken];
        const char* data = [deviceToken bytes];
    NSMutableString* token = [NSMutableString string];
    for (int i = 0; i < [deviceToken length]; i++) {
        [token appendFormat:@"%02.2hhX", data[i]];
    }
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://chargrilled.k-hosting.co.uk/test2/register_device.php?dt=%@",token]];
    NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]autorelease];
    NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:self];

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Device registered for push notification."
                                                 message:nil
                                                delegate:self
                                       cancelButtonTitle:nil
                                       otherButtonTitles:@"Okay", nil];
    [alert show];
    [alert release];

}
- (NSString*)stringWithDeviceToken:(NSData*)deviceToken {

}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Device registration failed."
                                                 message:nil
                                                delegate:self
                                       cancelButtonTitle:nil
                                       otherButtonTitles:@"Okay", nil];
    [alert show];
    [alert release];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    int i;
    for (id key in userInfo) {
        i++;
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
        if(i==2){
        }
    }
    application.applicationIconBadgeNumber = [[userInfo objectForKey:@"badge"] integerValue];
    [[[[[self tabBarController] viewControllers] objectAtIndex: 1] tabBarItem] setBadgeValue:@"new"];

}

检查您正在使用的配置文件,是否启用推送通知。

最新更新