How to handle push notifications if the application is already running?

Rahul Vyas picture Rahul Vyas · Oct 12, 2009 · Viewed 45.4k times · Source

How do we handle push notifications if the application is already running ? I want to show an alert if the application is running (instead of a push notification alert). Only if the application is not running, then show a push notification alert.

Also, if I send a payload to APNs, how can I create an alert with a cancel button?

Answer

notnoop picture notnoop · Oct 12, 2009

You can implement application:didReceiveRemoteNotification:

Here is a possible sample code:

- (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];
  }