lehn0058
lehn0058

Reputation: 20257

Method called when a push notification banner is selected

I have a push notification that I am sending to a user and I want to be able to take an action when they tap on it. I know that if the app is in the foreground, background, or if the user taps on the alert from the notification center that the following method is called in the app delegate:

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

However, if the app is not launched and the user taps on a notification banner as soon as the notification arrives, this method does not seem to get called. Is their a different method that I need to impliment in this situation? Are their other cases where other methods should be implemented as well?

Upvotes: 4

Views: 4769

Answers (1)

If you app is not launched when clicking on a notification banner, then you will receive an NSDictionary in your application:didFinishLaunchingWithOptions:.

Then you can just do something like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSDictionary *pushDict = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  if(pushDict)
  {
    [self application:application didReceiveRemoteNotification:pushDict];
  }
}

Additionally, in your application:didReceiveRemoteNtification: method, you can test if your application was inactive at the time the notification was received, like this:

-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
  if([app applicationState] == UIApplicationStateInactive)
  {
    NSLog(@"Received notifications while inactive.");
  }
  else
  {
    NSLog(@"Received notifications while active.");
  }

Upvotes: 4

Related Questions