当应用程序处于后台状态时,没有警报的APNS无法读取有效负载

时间:2012-07-03 12:36:47

标签: iphone background push-notification alert apple-push-notifications

任何人都可以帮助我使用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");  
}  
}   

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

1 个答案:

答案 0 :(得分: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............

}

isBackGroundModeglobal variable的位置。感谢