VidalRmrz
VidalRmrz

Reputation: 27

iOS - didRecieveRemoteNotification not working when app is not running

Push notifications are calling didReceiveRemoteNotification when the app is running or when it's in background. But if the user "kills" the app and I send a push notification, when the user taps on the notification, the app doesn't call didReceiveRemoteNotification.

This is what I have:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)dict{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:dict[@"title"]
                                                message:dict[@"description"]
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];

[alert show];

NSLog(@"didReceiveRemoteNotification: %@", dict);}

Upvotes: 0

Views: 213

Answers (1)

SierraMike
SierraMike

Reputation: 1569

According to UIApplicationDelegate protocol reference:

If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately.

When your app launches the applicationDidFinishLaunching will have data with any remote notifications received while the application was terminated. Use the code below when your application has finished launching.

if ([launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) 
{
   [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}

Source https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:

Upvotes: 3

Related Questions