1

我在装有 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 上没有?

非常感谢!!!提前

4

1 回答 1

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];
}
于 2012-07-02T09:13:22.917 回答