APN background refresh, setting up AppDelegate.m

2019-09-16 15:05发布

问题:

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!

回答1:

First of all, your second code block is a swift-Objective-C mix. So I'll assume that your developing using Objective-C.

So, the relevant method is the following:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
    if (application.applicationState == UIApplicationStateInactive) {
        // Application is inactive, fetch new data and call the completion handler.
        completionHandler(UIBackgroundFetchResultNewData);

    } else if (application.applicationState == UIApplicationStateBackground) {
        // Application is in background, fetch new data and call the completion handler.
        completionHandler(UIBackgroundFetchResultNewData);

    } else {
        // Application is active, so to what you need
        completionHandler(UIBackgroundFetchResultNewData);

    }
}

If you'll send the content-available = 1 in your notification object (inside the aps object), the notification will not be displayed to the user. This parameter tells the OS that you do not want to show new information to the user, but only to fetch new data from your server.

If you won't send this parameter, than the OS will treat it as a regular notification, and the above method will be called only after the user tap the displayed notification (unless the app is active, and than the notification won't be displayed, and this method will be called immediately).