Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle push notification in background in ios 10?

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.

like image 629
shraddha k vaishanani Avatar asked Jun 24 '17 06:06

shraddha k vaishanani


People also ask

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.

How do I manage push notifications on my iPhone?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered—immediately or in the scheduled notification summary.

What is background push notification?

A background push notification is a silent, remote notification that helps keep your app's data up to date. Unlike regular push notifications, background push notifications do not show app icon badges, trigger a sound, or display a message when received by your app.

Can you send local notifications while app is in background?

Notifications could be created at any moment (on Foreground, Background or even when the application is terminated/killed).


1 Answers

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 */
    }
like image 195
Abhishek Thapliyal Avatar answered Sep 17 '22 23:09

Abhishek Thapliyal