didReceiveRemoteNotification: fetchCompletionHandler: open from icon vs push notification
Ok I figured it out. The method is actually called twice (once when it receives the push, and once when the user interacts with the icon or the notification).
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if(application.applicationState == UIApplicationStateInactive) { NSLog(@"Inactive"); //Show the view with the content of the push completionHandler(UIBackgroundFetchResultNewData); } else if (application.applicationState == UIApplicationStateBackground) { NSLog(@"Background"); //Refresh the local model completionHandler(UIBackgroundFetchResultNewData); } else { NSLog(@"Active"); //Show an in-app banner completionHandler(UIBackgroundFetchResultNewData); }}
Thanks Tim Castelijns for the following addition:
Note: the reason it's called twice is due to the Payload having
content_available : 1
. If you remove the key and its value, then it will only run upon tapping. This will not solve everyone's problem since some people need that key to be true
@MikeV's solution in Swift 3 (but with switch statement):
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { switch application.applicationState { case .inactive: print("Inactive") //Show the view with the content of the push completionHandler(.newData) case .background: print("Background") //Refresh the local model completionHandler(.newData) case .active: print("Active") //Show an in-app banner completionHandler(.newData) }}
@MikeV's solution in Swift 2:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { if(application.applicationState == UIApplicationState.Inactive) { print("Inactive") //Show the view with the content of the push completionHandler(.NewData) }else if (application.applicationState == UIApplicationState.Background){ print("Background") //Refresh the local model completionHandler(.NewData) }else{ print("Active") //Show an in-app banner completionHandler(.NewData) }}