在ios5设备中未收到推送通知

时间:2012-07-02 08:02:08

标签: iphone ios5 ios4 apple-push-notifications

我在有iOS 5.0.1的设备上发送推送通知,但在iOS5设备上没有收到通知。

如果我尝试在iOS4.3.3设备上发送通知,则会在此设备上收到通知。

我的服务器是用c#开发的,苹果推送通知证书是以.p12格式使用的。

我注册通知的代码是

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Set the navigation controller as the window's root view controller and display.
    deviceToken = nil;


    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];


    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken 
{
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Succeeded in registering for APNS" message:@"Sucess" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertView show];
    [alertView release];

    deviceToken = [devToken retain];



    NSMutableString *dev = [[NSMutableString alloc] init];

    NSRange r;
    r.length = 1;
    unsigned char c;

    for (int i = 0; i < [deviceToken length]; i++)
    {
        r.location = i;
        [deviceToken getBytes:&c range:r];

        if (c < 10) {
            [dev appendFormat:@"0%x", c];
        }
        else {
            [dev appendFormat:@"%x", c];
        }

    }

    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

    [def setObject:dev forKey:@"DeviceToken"];

    [def synchronize];

    NSLog(@"Registered for APNS %@\n%@", deviceToken, dev);

    [dev release];

}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
{
    NSLog(@"Failed to register %@", [error localizedDescription]);

    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"FAILED" message:@"Fail" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertView show];
    [alertView release];

    deviceToken = nil;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"Recieved Remote Notification %@", userInfo);

    NSDictionary *aps = [userInfo objectForKey:@"aps"];
    NSDictionary *alert = [aps objectForKey:@"alert"];
    //NSString *actionLocKey = [alert objectForKey:@"action-loc-key"];
    NSString *body = [alert objectForKey:@"body"];


//  NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
//  
//  if ([viewControllers count] > 2) {
//      NSRange r;
//      r.length = [viewControllers count] - 2;
//      r.location = 2;
//      [viewControllers removeObjectsInRange:r];
//      
//      MapViewController *map = (MapViewController*)[viewControllers objectAtIndex:1];
//      [map askDriver];
//  }
//  
//  [self.navigationController setViewControllers:viewControllers];

//  [viewControllers release];

    //NewBooking,"BookingId",Passenger_latitude,Passenger_longitude,Destination_latitude,Destination_longitude,Distance,Time,comments

    NSArray *arr = [body componentsSeparatedByString:@","];
    if ([arr count]>0) {

        MapViewController *map = (MapViewController*)[[self.navigationController viewControllers] objectAtIndex:1];
        [map askDriver:arr];
        [self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:1] animated:YES];
    }



    //NSString *sound = [userInfo objectForKey:@"sound"];

    //UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:actionLocKey 
//                                                  message:body delegate:nil 
//                                        cancelButtonTitle:@"Reject" 
//                                        otherButtonTitles:@"Accept", nil];
//  
//  [alertV show];
//  [alertV release];


}

任何人都可以帮我解决这个问题。为什么在iOS4.3设备上收到通知但在iOS5上没收到?

非常感谢!!!提前

1 个答案:

答案 0 :(得分:1)

-(void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken   
{ 

    NSLog(@"~~~~devToken=%@",deviceToken); 


    NSString *dt = [[deviceToken description] stringByTrimmingCharactersInSet:     
    [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
    dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""]; 

    self.DeviceToken= dt;

    cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];  
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if (theConnection) 
    {
       NSLog(@"connection exists");
    }
    else   
    {
       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error" 
                                                   message:@"There was an error contacting the servers. Please try again." 
                                                  delegate:nil 
                                         cancelButtonTitle:@"OK" 
                                        otherButtonTitles:nil];
       [alert show];
       [alert release];
    }  
    [theConnection release]; 

}  

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo   
{


   NSLog(@"did receive remore notification %@",userInfo);
   if(isForground)
   { 
   // your code 
   }
}  

-(void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err  
{ 
   NSLog(@"Error in registration. Error: %@", err);


   NSString *status = [ NSString stringWithFormat: @"%@\nRegistration failed.\n\nError: %@",[err localizedDescription] ];

   UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:status  delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
   [alert release];
}