I want to trigger a data fetch when a push notification is received. I understand that this happens before a notification is displayed to the user. Right now I have this in my AppDelegate.m:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//restore push
[[NSNotificationCenter defaultCenter] postNotificationName:@"appRestorePush" object:nil];
}
else {
//push for when the app is open
self.pushString = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];
}
}
this controls what the app does depending on if the notification is received while the app is open in the foreground or if the app is open from a push notification. To add support for the background refresh do I just change this method to ?:
void didReceiveRemoteNotification (UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
if([content-available]) {
// fetch content methods here
completionHandler (UIBackgroundFetchResult.NewData);
}
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//restore push
[[NSNotificationCenter defaultCenter] postNotificationName:@"appRestorePush" object:nil];
}
else {
//push for when the app is open
self.pushString = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];
}
}
Will this work for both fetching the data before the push is displayed and then handle opening the app from a push like before or are there two separate methods, one for background refresh and one for handling how the push is opened? Any pointers would be really appreciated. Thanks!