Shaheer Palollathil
Shaheer Palollathil

Reputation: 313

Which method will be triggered for pushnotification when app is in background?

I have implemented push notification in my App. When App is in foreground didReceiveRemoteNotification method is get called. But when app is in background this method is not called. Following pattern is used in server side:

{ 
  aps: {
          content-available: 1,
          sound: "default"
       }
}

But still the didReceiveRemoteNotification is not get called. What else is to be done to get triggered the method after push notification arrives.

Upvotes: 0

Views: 5987

Answers (4)

Aniruddh
Aniruddh

Reputation: 7668

There's a new API available to handle background events when the device receives a push notification:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler

Now, as per the documentation:

Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method.

When this method is called, your app has up to 30 seconds of wall-clock time to perform the download operation and call the specified completion handler block. In practice, your app should call the handler block as soon as possible after downloading the needed data. If you do not call the handler in time, your app is suspended. More importantly, the system uses the elapsed time to calculate power usage and data costs for your app’s background downloads.

To trigger this method, you notification payload must contain a key content-available:

{
    "aps" : {
        "content-available" : 1
    },
    "content-id" : 42
}

Example Code:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"Remote Notification userInfo is %@", userInfo);

    NSNumber *contentID = userInfo[@"content-id"];
    // Do something with the content ID
    completionHandler(UIBackgroundFetchResultNewData);
}

Upvotes: 3

QUserS
QUserS

Reputation: 512

didReceiveRemoteNotification method will be called when push notification arrives in app active state. If app is inactive when push notification arrived, an option to invoke didReceiveRemoteNotification method is to click on the received notification from notification list and become active. If app is inactive when push notification arrived and become active by without clicking on notification received, normally there is no way to invoke didReceiveRemoteNotification method.

If you app needs, you can handle it by custom server. Whenever app becomes active, API call can be implemented to list pending notifications.

Upvotes: 2

Segev
Segev

Reputation: 19303

didReceiveRemoteNotification Will be called if your app is in UIApplicationStateActive

didReceiveRemoteNotification will be called if your app is inUIApplicationStateBackground or UIApplicationStateInactive and the user opened a push notification from the notification center.

didFinishLaunchingWithOptions will be called upon launch if app was not in the background.

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

    if (application.applicationState == UIApplicationStateActive )
    {
       //Your Code here
    }
    else if (application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive)
    {
        //Your code here
    }
}

Upvotes: 0

Rui Peres
Rui Peres

Reputation: 25927

This would be called:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

Check the launchOptions:

  NSDictionary *pushInformation = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  if(pushInformation)
  {
    // App opened with push notification
  }

Upvotes: 0

Related Questions