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
didReceiveRemoteNotification or didFinishLaunchingWithOptions do not get called in background after APN
When you open the app by tapping on the app icon you do not get any information about the push notification. Apple expects you to sync with your own server in that case.
Thus didReceiveRemoteNotification will never be called. If your app is terminated, didFinishLaunchingWithOptions will be called but you will not get any information about the push notification.
This behaviour does not make much sense to me personally, but to work around it we have created a notification syncing webservice whenever the app is opened.
Unable to get push (remote) notification when app icon tapped
You need to continue reading. The remainder of the section you quoted reads -
If the app icon is clicked on a computer running OS X, the app calls
the delegate’s applicationDidFinishLaunching: method in which the
delegate can obtain the remote-notification payload. If the app icon
is tapped on a device running iOS, the app calls the same method, but
furnishes no information about the notification.
Note the section I bolded. The short answer is that if the application is launched from the app icon then no information is provided about any notifications that may have been received.
App was opened from icon or notification
The application:didFinishLaunchingWithOptions:
paragraph of the SDK help explains very well which delegate methods are called in different start / wake up scenarios (for example clicking registered URL handler, opening a supported mime type, answering to remote / local notifications, clicking on the icon on the home screen, etc)
Also, if your application is already running at the time of the above events, the appropriate delegate methods will be called (for example didReceiveRemoteNotification
, openURL
, etc.) apart from applicationDidBecomeActive:
. From the combination of the called callbacks you can figure out which event happened.
Related Topics
Working with Live Photos in Playground
Calculate Total Traveled Distance iOS Swift
Dynamic Height for Static Table Cells with Wrapping Labels
How to Insert 40000 Records Fast into an SQLite Database in an iPad
Autolayout Complains About Constraints for 2 Uitextfields with No Borders
Recursivedescription Method in Swift
Integration New Facebook Sdk by Swift
Maximum Height of iOS 8 Today Extension
Disabling Nslog for Production in Swift Project
Uisearchbar: Clear Background Color or Set Background Image
Table Header View Height Is Wrong When Using Auto Layout, Ib, and Font Sizes
Storyboards VS. the Old Xib Way
iOS Designated Initializers:Using Ns_Designated_Initializer
Uiview Backgroundcolor Disappears When Uitableviewcell Is Selected