APN后台刷新,设置AppDelegate.m [英] APN background refresh, setting up AppDelegate.m

查看:256
本文介绍了APN后台刷新,设置AppDelegate.m的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想触发收到推送通知时取数据。据我了解,在这之前的通知显示给用户。现在,我有这在我的AppDelegate.m:

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];

        }
}

将这项工作为推前获取数据显示,然后处理来自一推打开的应用程序像以前一样,还是有两种不同的方法,一个是后台刷新,一个用于处理如何推动被打开?任何指针将是真正的AP preciated。谢谢!

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!

推荐答案

首先,你的第二个code块是斯威夫特 - Objective-C的组合。所以我会假设你使用Objective-C开发。

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);

    }
}

如果你发送内容可用 = 1在通知对象( APS 对象中)中,通知将不被显示给用户。这个参数告诉您不希望显示新的信息给用户,但只从服务器获取新数据OS。

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.

如果您不会发这个参数,比OS将会把它作为一个普通的通知,和上面的方法将被调用,用户点击显示的通知后,才(除非应用程序处于活动状态,并且比赢得通知'T显示,这种方法将被立即调用)。

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).

这篇关于APN后台刷新,设置AppDelegate.m的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆