How to handle push notification in background in ios 10?

13,283

Solution 1

willPresentNotification is called when app is in foreground. Have a look to their documentation

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    // The method will be called on the delegate only if the application is in the foreground.
    // If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented.
    // The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list.
    // This decision should be based on whether the information in the notification is otherwise visible to the user.

}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)())completionHandler {
    // The method will be called on the delegate when the user responded to the notification by opening the application,
    // dismissing the notification or choosing a UNNotificationAction.
    // The delegate must be set before the application returns from applicationDidFinishLaunching:.

}

Try to check in didReceiveNotificationResponse you will get what you need.

ALSO If need to fetch any data or any processing, Enable background fetch in background modes and use below method

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{

    completionHandler(UIBackgroundFetchResultNewData);
}

Handling APNS on the basis of application states

   if(application.applicationState == UIApplicationStateInactive)
     {
        /* 
        # App is transitioning from background to foreground (user taps notification), do what you need when user taps here!
         */    
    }
    else if(application.applicationState == UIApplicationStateActive)
    {
        /*
         # App is currently active, can update badges count here
       */
    }
    else if(application.applicationState == UIApplicationStateBackground)
    {
        /* # App is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here */
    }

Solution 2

When a push notification containing the alert key is received iOS will present the notification to the user. If the user interacts with the alert your application will be launched into the foreground mode.

Silent notifications are push notifications that contain the content-available key. These notifications are not presented to the user and can't contain the alert, sound, or badge keys. When iOS delivers a silent notification your application is launched into the background mode. The application can perform a very limited amount of work in the background in response to the silent notification.

A silent notification looks like this:

{
    "aps" : {
        "content-available" : 1
    },
    "com.yourcompany.something.something" : "something"
}

When it is delivered the application delegate method application:didReceiveRemoteNotification:fetchCompletionHandler: is called. Your implementation of that method has 30 seconds to call the block passed in the fetchCompletionHandler parameter.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {  
    completionHandler(UIBackgroundFetchResultNoData);
    return;
}

Solution 3

This was the solution for me:

{  
    "aps" : {  
        "content-available" : 1  
    },  
    "acme1" : "bar",  
    "acme2" : 42  
}  

What is important is to put the content-available to 1.

Share:
13,283

Related videos on Youtube

shraddha k vaishanani
Author by

shraddha k vaishanani

Updated on July 05, 2022

Comments

  • shraddha k vaishanani
    shraddha k vaishanani almost 2 years

    I am not handle push notification in background.

    For Handle push notification in background following below steps :-

    1. In Capabilities -> Enable Remote notification.
    2. In Capabilities -> Background Mode -> Enable Remote notifications.
    3. In didFinishLaunchingWithOptions give all permission for ios 10.
    4. For push notification used UNUserNotificationCenter.
    5. App In Foreground then push notification is working fine and below method call :

      userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
      

    But my problem is app in background then not call any method.so any one have idea or solution for handle push notification in background for ios 10 then please help me.

    Thanks.

  • shraddha k vaishanani
    shraddha k vaishanani almost 7 years
    hello abhishek, didReceiveRemoteNotification is called in ios 10?
  • shraddha k vaishanani
    shraddha k vaishanani almost 7 years
    Hello abhishek, i tried your solution but it's also not working so any other solution then please help me.
  • Abhishek Thapliyal
    Abhishek Thapliyal almost 7 years
    yes for silent notifications will be called didReceiveRemoteNotification
  • Abhishek Thapliyal
    Abhishek Thapliyal almost 7 years
    @DivyaPatel Also enable background fetch and try again
  • shraddha k vaishanani
    shraddha k vaishanani almost 7 years
    your answer is working proper but i have one question. suppose App in Background and user tap on notification then didReceiveNotificationResponse called but without any tap open application and remove notification banner then how to manage Push Notification? if any idea then help me.
  • Abhishek Thapliyal
    Abhishek Thapliyal almost 7 years
    @DivyaPatel : i have updated my answer for that if its working u can upvote me :)
  • shraddha k vaishanani
    shraddha k vaishanani almost 7 years
    Thank you abhishek it's working but sorry i am not able to upvote your answer because i have not 50 reputation.
  • chetan patel
    chetan patel over 6 years
    @AbhishekThapliyal. can you please look at this question ? stackoverflow.com/questions/46212370/…
  • Moxarth
    Moxarth over 6 years
    hi , i have a similar scenario but slightly modification required , like when i get the push notification ,the app should get active in background and do the task given in , ( like user's location update code ) ,- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo , but my app would not get active until user taps on the notification . so , how would i get this done while app is in background ? dose anyone know the solution for this ?
  • ΩlostA
    ΩlostA almost 6 years
  • ΩlostA
    ΩlostA almost 6 years
    @Moxarth Put content-available to 1 : { "aps" : { "content-available" : 1 }, "acme1" : "bar", "acme2" : 42 }