设备始终显示无法注册推送通知

时间:2011-12-05 06:53:31

标签: iphone objective-c

我在我的应用中使用了推送通知服务。

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

我使用以下代码进行推送通知:

任何人都可以在我出错的地方帮助我。

提前致谢。

- (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"];


}

1 个答案:

答案 0 :(得分:3)

检查您正在使用的配置文件,是否启用推送通知。如果没有使用推送通知创建新的。