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 に答える