application: didReceiveRemoteNotification: fetchCompletionHandler:
is different from
application: didReceiveRemoteNotification:
The Apple docs are a bit confusing
application: didReceiveRemoteNotification: fetchCompletionHandler:
is used if your application supports the remote-notification background mode (ie you're doing BackgroundFetch.)
application: didReceiveRemoteNotification:
is called when the OS receives a RemoteNotification and the app is running (in the background/suspended or in the foreground.)
You can check the UIApplicationState to see if the app was brought to foreground by the user (tapping on notification) or was already running when notification comes in.
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
// user tapped notification while app was in background
if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
// go to screen relevant to Notification content
} else {
// App is in UIApplicationStateActive (running in foreground)
// perhaps show an UIAlertView
}
}
I'm not sure if I understand your question.
Do you want to differentiate between a silent push notification background fetch and a noisy push notification? You can simply check whether the push notification dictionary contains the "content-available" key: [[userInfo objectForKey:@"aps"] objectForKey:@"content-available"]
If it does, then it should be a silent push. If not, it was a normal push.
Do you want to know if that background fetch method is called when the application receives a notification and it is in suspended/not running? If so, you can do the following:
Put this in any method you want to see whether/when that method is invoked:
DDLogDebug(@"%@ - %@",NSStringFromSelector(_cmd),NSStringFromClass([self class]));
This will print the class and the method to the log file.
Examine the log file after sending yourself a push notification to your background-fetch enabled app, and see if any of the methods get called by looking at your log file.
If you have set up your app correctly for background fetch, the method application: didReceiveRemoteNotification: fetchCompletionHandler:
will be called even when the app is backgrounded/not running if you receive a push notification (silent push or not).
For Swift: In application(_:didFinishLaunchingWithOptions:)
parse the application options. If they exist, you know the app was launched from them tapping.
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] {
print("Remote notfi is \(remoteNotif)")
if let notification = remoteNotif["aps"] as? [AnyHashable : Any] {
/// - parse notification
}
}
Otherwise, you can handle the tap in, and you know that the app is open/background/inactiveapplication(_:didReceiveRemoteNotification:fetchCompletionHandler:)
When application: didReceiveRemoteNotification:fetchCompletionHandler:
method is called app state is UIApplicationStateInactive
if user taps on alert (in this case you would like to prepare some UI) and is UIApplicationStateBackground
when app is silently woken (in this case you just load some data).
In iOS 10.0+ you can use the method
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler;
to detect when user taps on a system-displayed alert from the NotificationCenter.
If you implement the method above
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
will be called only when a notification is received- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler;
will be called when user taps on notificationApplication State is not reliable because if you have control center or Apple's notification center open over your app, application: didReceiveRemoteNotification: fetchCompletionHandler: will get called and the application state will be Inactive.
I'm having the same issue trying to respond to a click on the notification while the app is in the background and there doesn't seem to be a reliable way to solely identify this.