0
votes

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.

4
Is there user interaction with push notification ? or your app just sits and waiting in background ?Janak Nirmal
Please note that until any user interaction is performed, no method will be called when the application is in minimised state.Aniruddh
I guess you are talking about -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler method.Toseef Khilji
@JanakNirmal No App simply sits in background. Method is called when we tap notification alert on device banner. but i am asking the situation when there is no user interaction.Shaheer Palollathil
We don't have much control over the app when it is at minimised state and absolutely no control over it when we receive a push and the application under minimised state. We need user interaction in your particular case.Aniruddh

4 Answers

3
votes

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);
}
2
votes

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.

0
votes

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
  }
0
votes

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
    }
}