当应用程序在后台运行时(例如,从服务器提取内容),无需APN即可重复执行任务 [英] Repeating a task when app is in background (e.g. pull something from a server), without APNs

查看:121
本文介绍了当应用程序在后台运行时(例如,从服务器提取内容),无需APN即可重复执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有苹果认可的良好替代方法,可以代替使用推送通知来触发应用程序定期运行某些代码(特别是:从服务器提取新消息)?

Is there a good, Apple-approved alternative to using push notifications to trigger an app to run some code (in particular: pull new messages from a server) in a regular interval?

  • 伪装成VoIP应用不是一个好选择(不会获得批准,请参见 iPhone:重复背景任务)
  • 我无法使用位置更新,如果用户不走动,它应该可以工作

在模拟器中,将beginBackgroundTaskWithExpirationHandlerdispatch_async一起使用会给您10分钟的背景时间,我发现如果背景自行重启",则backgroundTimeRemaining属性似乎总是​​会重置为10分钟.这是代码.

In the simulator, using beginBackgroundTaskWithExpirationHandler with dispatch_async gives you 10 minutes of background time, and I found out that if the background "restarts itself", the backgroundTimeRemaining property always seems to be reset to 10 minutes. Here's the code.

- (void) work
{
    UIApplication *application = [UIApplication sharedApplication];

    NSLog(@"bg %@ (T-%.1f seconds)",
          [NSDate date],
          [application backgroundTimeRemaining]);
    sleep(10);

    [application endBackgroundTask:_bgTask];
    _bgTask = UIBackgroundTaskInvalid;

    [self startTask];
}

- (void)startTask
{
    UIApplication *application = [UIApplication sharedApplication];
    _bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"expired at %@", [NSDate date]);

        [application endBackgroundTask:_bgTask];
        _bgTask = UIBackgroundTaskInvalid;
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,
                                             0),
                   ^{ [self work]; });
}

- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    /* ... */

    [self startTask];

    /* ... */
}

在后台运行某些东西会被接受吗?我的意思是,大多数时候我只想sleep(...),直到我想重复一个网络请求或做一些有用的事情,但是该应用程序永远不会真正进入完整的后台模式.有没有人有这种方法的经验?提示:仅在模拟器上经过测试,而没有其他正在运行的应用程序.

Would that be an accepted practice to run something in the background? I mean, most of the time I would just sleep(...) until I want to repeat a network request or do something useful, but the app would never really enter full background mode. Does anyone have experience with this approach? Hint: Only tested on simulator without other running apps.

推荐答案

查看表3-1应用程序的后台模式",从服务器重复获取信息的两个相关替代方法是后台获取"或远程通知".

Looking at "Table 3-1 Background modes for apps", the two relevant alternatives for repeatedly getting info from a server are "Background fetch" or "remote-notification".

远程通知"是推送通知,您说您不想使用它.

"remote-notification" is push notifications, which you say you don't want to use.

因此,背景获取"是相关的选择.例如,请参见 https://blog.newrelic.com/2016/01/13/ios9-background-execution 机会性下载远程内容".

Therefore, "Background fetch" is the relevant choice. For example, see https://blog.newrelic.com/2016/01/13/ios9-background-execution "Downloading remote content opportunistically".

但是,这不能给您(开发人员)如Android那样的控制程度.有"setMinimumBackgroundFetchInterval",但是请注意,这是一个最小值:iOS决定何时为下一次后台获取调用您的应用程序. (Apple专注于总体电池使用量和设备响应速度;一旦您的应用进入后台,设计便会正常运行,而很少受到关注.)

However, this does not give you (the developer) the degree of control you would have in Android. There is "setMinimumBackgroundFetchInterval", but notice that this is a MINIMUM: iOS decides when to call into your app for the next background fetch. (Apple is focused on overall battery usage and device responsiveness; once your app is in the background, design to work gracefully with however little attention it is given.)

注意:如果用户终止了您的应用程序,则背景获取"将随之终止.这是设计使然. (从用户的角度来看,这是一件好事:大多数应用程序如果被杀死,应该保持死状态.)

NOTE: If the user kills your app, "Background fetch" will be killed with it. This is by design. (And is a good thing, from the user's viewpoint: most apps should stay dead if killed.)

考虑结合使用推送通知"(远程通知)和背景获取".例如,如果用户允许您的应用推送通知",则每天进行一次推送,同时向用户发送文本通知.如果他们打开该通知,则将打开您的应用程序.然后开始数据获取.如果他们按了主页"按钮,请使用背景获取"功能来继续全天定期获取数据.

Consider using a combination of "push notification" (remote-notification) and "Background fetch". For example, if a user permits "push notifications" by your app, then do one push daily, with a text notification to user. If they open that notification, that will open your app. Then begin the data fetching. If they hit Home button, use "Background fetch" to continue data fetching periodically through the day.

非常注重电池使用的用户可能会定期终止其所有应用程序.如果他们今天不想被您的应用打扰,他们将忽略或删除您的应用的推送通知.认为这是一件好事:在用户不积极使用您的应用的日子里,您不会因为耗尽电量而使用户烦恼.

Users who are very battery-conscious may kill all their apps periodically. If they don't want to be bothered by your app today, they will ignore or delete your app's push notification. Consider this a good thing: you won't annoy users by draining battery on days that they aren't actively using your app.

想要严格控制手机上正在运行的内容的用户将不会允许您的应用程序推送通知. (例如,我讨厌每天收到文本通知.不要给我打电话,我会给你打电话.)在这种情况下,一旦您的应用被杀死,您将无能为力(因为推送通知是唯一的方法恢复死了的应用,我(用户)对此表示否".请务必考虑您将如何为此类用户提供服务. (当您第一次重新打开您的应用时,您的内容可能会过时.)

Users who like to have tight control over what is running on their phone WILL NOT permit push notifications for your app. (For instance, I hate to receive daily text notifications. Don't call me, I'll call you.) In this case, once your app is killed, there is nothing that you can do (since push notification is the only way to resurrect your dead app, and I, the user, have said "No" to that). Be sure to consider how you will serve such users. (Your content may be stale when they first re-open your app.)

这篇关于当应用程序在后台运行时(例如,从服务器提取内容),无需APN即可重复执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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