2011-12-05 121 views
0

在我的應用程序中使用推送通知服務。設備總是顯示無法註冊推送通知

但它現在總是顯示(當應用程序啓動時)它未能註冊pushnotification。

我用下面的代碼推送通知:

誰能幫我,我要去哪裏錯了。

在此先感謝。

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


} 

回答

3

檢查您正在使用的配置文件,是否啓用推送通知。如果不是通過推送通知創建新的。

+0

,是的..正確。謝謝你.. – ishhhh

相關問題