Gaurav Singla
Gaurav Singla

Reputation: 2291

Handle Push Payload when the App is Exited or Suspended

I have done with all the cases except one while receiving Push Payload.

Here is my Implementation :

If the Application is in Background, inActive or Active State then we can receive Notification with this Method.

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
       //Received Notification when the Application is Active, InActive or in Background State. 
       // NOTE : I am Handling below line of code with the Appropriate Conditions using Application States in my Code.
       [self handleNotificationDataWithDictionary:dictionary updateUI:YES];
}

and If the Application is Suspended or Exited then we can receive the Notification Payload in didFinishLaunchingWithOptions Method but only in the case when user Taps on the Notification.

Here is the Implementation for receiving payload when app is exited or suspended and opened with the Tap of Push Notification from Home Screen or Lock Screen,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
       if (launchOptions != nil)
       {
           NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
           if (dictionary != nil)
           {
               [[[UIAlertView alloc] initWithTitle:@"Finish" message:@"Test" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil] show];
               NSLog(@"Launched from push notification: %@", dictionary);
              [self handleNotificationDataWithDictionary:dictionary updateUI:NO];
           }
       }
}

Now the Final Case is, When the Application is suspended and User didn't open the Application using the Notification Center rather opened the App by Tapping the Application Icon. As I tested my code, I am not able to receive the Payload in this case. I googled a lot but found no solution on this Case.

As per Application docs, we can get Payload only in Background or InActive Mode when the App is not Active. Then how to implement it for Suspended State ?

I really need help on this, Please post your best Answers so that it might useful for others too.

Upvotes: 1

Views: 464

Answers (2)

Rohit Kumar
Rohit Kumar

Reputation: 887

hris.to is absolutely right, if you want your custom notification panel inside your app, you have to build an API and then fetch once again from the server for the latest notifications.

Upvotes: 1

hris.to
hris.to

Reputation: 6363

Simply put - if user taps on App Icon your push is gone! No way to retrieve that info.

Official Documentation about this:

If the app icon is tapped on a device running iOS, the app calls the same method, but furnishes no information about the notification.

Upvotes: 2

Related Questions