web-dev-qa-db-fra.com

Comment gérer les notifications Push si l'application est déjà en cours d'exécution?

Comment traitons-nous les notifications Push si l'application est déjà en cours d'exécution? Je souhaite afficher une alerte si l'application est en cours d'exécution (au lieu d'une alerte de notification push). Uniquement si l'application n'est pas en cours d'exécution, affichez une alerte de notification Push.

De plus, si j'envoie une charge utile aux APN, comment puis-je créer une alerte avec un bouton d'annulation?

27
Rahul Vyas

Vous pouvez implémenter application:didReceiveRemoteNotification:

Voici un exemple de code possible:

- (void)application:(UIApplication *)application
   didReceiveRemoteNotification:(NSDictionary *)userInfo
{
  NSString *message = nil;
  id alert = [userInfo objectForKey:@"alert"];
  if ([alert isKindOfClass:[NSString class]]) {
    message = alert;
  } else if ([alert isKindOfClass:[NSDictionary class]]) {
    message = [alert objectForKey:@"body"];
  }
  if (alert) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                       message:@"AThe message."  delegate:self
                             cancelButtonTitle:@"button 1"
                             otherButtonTitles:@"button", nil];
    [alertView show];
    [alertView release];
  }
52
notnoop

Vous pouvez vérifier l'état de l'application UIA. Faites juste une vérification comme celle-ci

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

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive)
    {

            UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"xxx" message:yourMessage delegate:self cancelButtonTitle:@"Done" otherButtonTitles: @"Anzeigen", nil] autorelease];
            [alert setTag: 2];
            [alert show];
    }
    else {
        // Push Notification received in the background
    }
}
45
TomTom

La touche "alerte" ne sera pas là directement sous le dictionnaire userInfo, vous devez obtenir un autre dictionnaire avec le nom "aps" et ensuite obtenir "alerte" ou "corps" du dictionnaire "aps".

14
Vijay Shankar

Itération des 3 niveaux de charge utile

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

for (id key in userInfo) {
    NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    NSString *message = nil;

NSDictionary *aps = [NSDictionary dictionaryWithDictionary:(NSDictionary *) [userInfo objectForKey:key] ];   
    for (id key1 in aps){
         NSLog(@"key1: %@", key1);
        id alert = [aps objectForKey:key1];
        if ([alert isKindOfClass:[NSDictionary class]]) {
            message = [alert objectForKey:@"body"];
             NSLog(@"body: %@, value: %@", key1, message);
            message = [alert objectForKey:@"loc-args"];
            NSLog(@"loc-args: %@, value: %@", key1, message);
            NSArray *args = (NSArray *) [alert objectForKey:@"loc-args"] ;
                for (id key2 in args){
                    NSLog(@"key2: %@, value: ", key2);
                }
            message = [alert objectForKey:@"action-loc-key"];
            NSLog(@"action-loc-key: %@, value: %@", key1, message);

        }
        else if ([alert isKindOfClass:[NSArray class]]) {
            for (id key2 in key1){
                NSLog(@"key2: %@, value: %@", key2, [key1 objectForKey:key2]);
            }
        }
        else if([key1 isKindOfClass:[NSString class]]) {
            message = [aps objectForKey:key1];
            NSLog(@"key1: %@, value: %@", key1, message);
        } 

    }
  } 

}

Le résultat est:

2012-01-27 20:38:09.599 SPush[4181:707] key: aps, value: {
alert =     {
    "action-loc-key" = Open;
    body = test;
    "loc-args" =         (
        1000,
        2000
    );
};
badge = 0;
"content-available" = 10;
sound = default;
}
2012-01-27 20:38:13.133 SPush[4181:707] key1: alert
2012-01-27 20:38:13.134 SPush[4181:707] body: alert, value: test
2012-01-27 20:38:13.137 SPush[4181:707] loc-args: alert, value: (
1000,
2000
)
2012-01-27 20:38:13.138 SPush[4181:707] key2: 1000, value: 
2012-01-27 20:38:13.139 SPush[4181:707] key2: 2000, value: 
2012-01-27 20:38:13.140 SPush[4181:707] action-loc-key: alert, value: Open
2012-01-27 20:38:13.141 SPush[4181:707] key1: sound
2012-01-27 20:38:13.143 SPush[4181:707] key1: sound, value: default
2012-01-27 20:38:13.144 SPush[4181:707] key1: badge
2012-01-27 20:38:13.145 SPush[4181:707] key1: badge, value: 0
2012-01-27 20:38:13.146 SPush[4181:707] key1: content-available
2012-01-27 20:38:13.147 SPush[4181:707] key1: content-available, value: 10
5
Fahar