简体   繁体   English

使用UILocalNotifications,具有完成处理程序和后台提取的远程通知

[英]Working with UILocalNotifications, Remote Notifications with completion handler and Background Fetch

With iOS7, it is possible to have completionHandler for Remote Notifications. 使用iOS7,可以使用completionHandler进行远程通知。 Is it possible to do the same for UILocalNotifications? 是否可以为UILocalNotifications做同样的事情?

Basically, I want a webservice to post my some data at regular time intervals of 30 seconds, even if the app is in background. 基本上,我希望web服务以30秒的固定时间间隔发布我的一些数据,即使应用程序在后台也是如此。 For this I considered 3 options but didn't get help from any : 为此我考虑了3个选项,但没有得到任何帮助:

  1. Background Fetch : This will work in background, but I can't use it as it is not mandatory that iOS will always invoke this background fetch at my desired time intervals. 后台提取:这将在后台运行,但我不能使用它,因为iOS不会强制要求我按照所需的时间间隔调用此后台提取。

  2. Remote Notifications : This works perfectly. 远程通知:这非常有效。 But every 30 seconds I have to post a Remote PUSH Notification, which is not at all practical. 但是每30秒我就要发一个远程PUSH通知,这根本不可行。 Also it'll be great if I could handle it locally. 如果我能在当地处理它也会很棒。

  3. UILocalNotifications : There's no completion handler for this. UILocalNotifications:没有完成处理程序。 User WILL HAVE TO open the app. 用户必须打开该应用程序。 So this ain't working as well! 所以这不行!

Are there any other options? 还有其他选择吗? Or even with iOS7, it's still not possible to do something locally in background? 或者即使使用iOS7,仍然无法在后台本地执行某些操作?

Please help. 请帮忙。 Thanks! 谢谢!

You covered all the options, and as you see, this isn't supported. 您已涵盖所有选项,如您所见,这不受支持。 For good reasons, mostly, as waking up the application in the background is a costly operation (in iOS7, a snapshot is taken after apps finish their background work), doing it every 30 seconds would be devastating to the battery life. 出于好的理由,主要是因为在后台唤醒应用程序是一项代价高昂的操作(在iOS7中,应用程序完成后台工作后拍摄快照),每30秒执行一次会对电池寿命造成破坏。

Seeing as you haven't mentioned what data you need to post, in most cases I would suggest you redesign your app to be friendly to your users' batteries. 看到你没有提到你需要发布的数据,在大多数情况下,我建议你重新设计你的应用程序,以便对用户的电池友好。 If you need location reporting, consider listening to significant location changes instead of recording every 30 seconds. 如果您需要位置报告,请考虑收听重要的位置更改,而不是每30秒记录一次。

Abusing push notifications is possible (note, that silent remote notifications are rate-limited by Apple), but consider the experience your users will have. 滥用推送通知是可能的(请注意,Apple默认远程通知受到速率限制),但请考虑您的用户将拥有的体验。

If you feel that this feature should be there and is missing, you should open an enhancement request with Apple and post the radar number here so people can duplicate it. 如果您认为此功能应该存在且缺失,则应该向Apple打开增强请求并在此处发布雷达编号,以便人们可以复制它。

Background fetch is a direct answer for your problem. 后台提取是您问题的直接答案。 Background fetch initiates your fetch handler whenever the iOS is free to execute a task periodically. 只要iOS可以定期执行任务,后台提取就会启动您的提取处理程序。 All you need to do is initiate you NSURLSession request in 您需要做的就是启动NSURLSession请求

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

NSURLConnection is no longer a valid API for service calls or nor it supports background tasking. NSURLConnection不再是服务调用的有效API,也不支持后台任务。 NSURLSession is clearly a replacement API for NSURLConnection with its advanced iOS 7 benefits. NSURLSession显然是NSURLConnection的替代API,具有iOS 7的高级优势。 Adding below, documentation from Apple's iOS documentation 下面添加Apple的iOS文档中的文档

 NSURLSession is a replacement API for NSURLConnection.  It provides
 options that affect the policy of, and various aspects of the
 mechanism by which NSURLRequest objects are retrieved from the
 network.

Background fetch interval can be set to define the repetition frequency for background fetch, but it also considers factors of the OS resources and pending operations 可以设置后台获取间隔以定义后台获取的重复频率,但它也会考虑OS资源和待处理操作的因素

- (void)setMinimumBackgroundFetchInterval:(NSTimeInterval)minimumBackgroundFetchInterval;

iOS 7 clearly gives a better way to update content of the application, but it respects device resources & user priority as iOS generally does. iOS 7显然提供了更新应用程序内容的更好方法,但它尊重设备资源和用户优先级,就像iOS一般。 Hope this answers your question. 希望这能回答你的问题。

Actually, all looks like you can't communicate with webservice at regular time intervals from background. 实际上,所有看起来都无法从后台以固定的时间间隔与webservice通信。

UILocalNotifications or Remote Notifications need user's action to wake up the app if it's backgrounded. UILocalNotifications或远程通知需要用户的操作来唤醒应用程序,如果它是后台的。 EDITED: Starting from iOS 7.0 remote notification can wake up the app but it's not a flexible solution 编辑:从iOS 7.0开始远程通知可以唤醒应用程序,但它不是一个灵活的解决方案

iOS allows more activities in background for applications that use one of specified UIBackgroundModes, see please "Table 3-4 Background modes for apps": 对于使用指定UIBackgroundMode之一的应用程序,iOS允许在后台进行更多活动,请参阅“表3-4应用程序的背景模式”:

Here is a link to related Apple docs 以下是相关Apple文档的链接

If your application isn't positioned for one of bg modes directly, I agree with Anil Kumar (post above) that background fetch is the most useful thing here. 如果你的应用程序没有直接定位于bg模式之一,我同意Anil Kumar(上面的帖子),这里背景提取是最有用的。 However it doesn't do completely what you need. 但它并不能完全满足您的需求。 [UIApplication setMinimumBackgroundFetchInterval:] it doesn't mean any strict time. [UIApplication setMinimumBackgroundFetchInterval:]它并不意味着任何严格的时间。 It means minimum interval only (or desired time). 它仅表示最小间隔(或所需时间)。 Here is more info: 这是更多信息:

performFetchWithCompletionHandler never gets fired performFetchWithCompletionHandler永远不会被触发

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM