0

任何人都可以帮助我使用 APNS。问题是当没有给出警报时,我只能在运行状态而不是在后台状态接收有效负载。当应用程序处于后台和运行状态时,我能够收到通知和有效负载。

- (void)applicationDidBecomeActive:(UIApplication *)application  
    {  
NSLog(@"entered the condition applicationDidBecomeActive");    
if ( application.applicationIconBadgeNumber == 0 ){  
    NSLog(@"app was already in the foreground");  
}  
else{  
    NSDictionary *userInfo;  
    NSDictionary *apsDic = [userInfo valueForKey:@"aps"];  
    NSString *alertStr = [apsDic valueForKey:@"alert"];  
    NSLog(@"alertstring =%@",alertStr);  
    NSString *msgStr = [userInfo valueForKey:@"m"];  
    NSLog(@"messtring= %@",msgStr);  
    NSNumber *badgeNum = [apsDic valueForKey:@"badge"];  
    NSLog(@"Badge Number = %@",badgeNum);  
    NSLog(@"app was already in the background");  
}  
}   

问题是每次它说“应用程序已经在前台”时,它都无法读取徽章编号

4

1 回答 1

0

尝试这个:-

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    {
         NSLog(@"remote notification: %@",[userInfo description]);


        if (userInfo) 
        {
            NSLog(@"%@",userInfo);

            if ([[userInfo allKeys] containsObject:@"aps"]) 
            { 
                if([[[userInfo objectForKey:@"aps"] allKeys] containsObject:@"alert"]) 
                {
                        NSDictionary *apsDic = [userInfo valueForKey:@"aps"];  
                        NSString *alertStr = [apsDic valueForKey:@"alert"];  
                       NSLog(@"alertstring =%@",alertStr);  
                        NSString *msgStr = [userInfo valueForKey:@"m"];  
                       NSLog(@"messtring= %@",msgStr);  
                       NSNumber *badgeNum = [apsDic valueForKey:@"badge"];  
                       NSLog(@"Badge Number = %@",badgeNum); 

                    if(!isBackGroundMode)
                    {

                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"HEY!"] message:[pushDic objectForKey:@"alert"] delegate:self cancelButtonTitle:@"Luk" otherButtonTitles:@"Læs..",nil];
                        [alert show];
                        [alert setTag:1];
                        [alert release];

                    }

                    else 
                    {
                        [self performSelectorOnMainThread:@selector(directGoToDetalPage) withObject:nil waitUntilDone:NO];
                    }


                }

            }

        } 
    }
- (void)applicationDidEnterBackground:(UIApplication *)application {

    isBackGroundMode = TRUE;
}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    isBackGroundMode = FALSE;

}
#pragma mark 
#pragma mark(alertView delegte) Method

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag==1)
    {
        if(buttonIndex == 1)
        {
            [self performSelectorOnMainThread:@selector(directGoToDetalPage) withObject:nil waitUntilDone:NO];

        }
    }
}

#pragma mark 
#pragma mark(when PushNotification Come) Method

-(void) directGoToDetalPage
{
    // where you want to go page............

}

isBackGroundMode在哪里global variable。谢谢

于 2012-07-03T12:44:23.823 回答