简体   繁体   English

如何在后台和数据同步中获取通知

[英]How to get notifications in background and data synchronization

I have here some requirements and now I want to check if these requirements are possible and how they could be reached. 我在这里有一些要求,现在我想检查这些要求是否可行以及如何达到这些要求。 It is about notifications and running in background: 它是关于通知和在后台运行:

  • The app should show notifications if new data is available and the app is in background (if the app is not running there should be no notifications) 如果新数据可用且应用程序处于后台,应用程序应显示通知(如果应用程序未运行,则应该没有通知)
  • Also the app should synchronize the data so that the data is up-to-date 此外,应用程序应同步数据,以便数据是最新的
  • The user should be able to choose an interval when the synchronization should take place (5 seconds, 30 seconds, ...) 用户应该能够选择应该进行同步的间隔(5秒,30秒......)
  • The app should run on iOS 6 and above. 该应用程序应在iOS 6及更高版本上运行。

Notifications: 声明:
The problem with local notifications is that you only can set seconds, minutes, hours ... as interval. 本地通知的问题是您只能将秒,分钟,小时......设置为间隔。 It is not possible to set a custom interval. 无法设置自定义间隔。 But this type would only be appropriate if I have a background task, which searches for updates and if an update is available, it immediatetly displays a local notification. 但是这种类型只适用于我有后台任务,它会搜索更新,如果有更新,它会立即显示本地通知。

Remote notifications on the other side needs an additional server for the notifications. 另一方面的远程通知需要额外的服务器来进行通知。 Also the port 5223 has to be open to receive the notifications (but most of the time cellular network is used and here the port should be open). 此外,端口5223必须是开放的以接收通知(但是大多数时候使用蜂窝网络并且这里端口应该是打开的)。 How many remote notifications can be send and presented to the user? 可以向用户发送和呈现多少个远程通知? For local notifications it is 64. 对于本地通知,它是64。

Running in background: 在后台运行:
Only in iOS 7 Background Fetch and Remote Notficiations are available. 仅在iOS 7中提供Background FetchRemote Notficiations显示。 The problem with the Background Fetch is that only small data should be transferred and the OS determines when the app gets a timeslot (own algorithm which learns from the user). Background Fetch的问题在于,只应传输小数据,并且操作系统确定应用程序何时获得时间段(自己的算法从用户学习)。 Sources reports a max. 消息来源报告最多 execution time from 30 seconds, but I didn't found an official statement from Apple. 执行时间从30秒开始,但我没有找到Apple的官方声明。

Remote Notifications also seems to need a provider. Remote Notifications似乎也需要提供者。 But here the user would get a notification and the app could download/sync the new data in background (again max. 30 seconds). 但是在这里,用户会收到通知,应用程序可以在后台下载/同步新数据(同样最多30秒)。

What about iOS 6? iOS 6怎么样?

Another option would be to let the app run in background / have a background thread which checks for updates/synchronize at a certain intervall and than creates a local notification. 另一个选择是让app在后台运行/有一个后台线程,它检查某个intervall的更新/同步,然后创建一个本地通知。 But how long does the app run in background? 但该应用程序在后台运行多长时间? I read about 5 seconds which would be to short. 我读了大约5秒,这将是短暂的。

And another one would be to use push notifications and if the user starts the app it does the synchronization process. 另一个是使用推送通知,如果用户启动应用程序则执行同步过程。

beginBackgroundTaskWithExpirationHandler seems not to suite here for my case. beginBackgroundTaskWithExpirationHandler似乎不适合我的情况。

How would I get notifications when running in background together with synchronization? 如何在后台运行并同步时收到通知?

As you probably know, backgrounding on iOS is rather limited. 您可能知道,iOS上的后台处理相当有限。 iOS will not let your application just run in background indefinitely. iOS不会让您的应用程序无限期地在后台运行。 If you use beginBackgroundTaskWithExpirationHandler , your app will get some background runtime (10 minutes on iOS 6, 3 minutes on iOS 7), but you can't "renew" that time while app is still background. 如果您使用beginBackgroundTaskWithExpirationHandler ,您的应用程序将获得一些后台运行时(iOS 6上10分钟,iOS 7上3分钟),但是当应用程序仍然是后台时,您无法“更新”该时间。 Thus, your second option ("let the app run in background / have a background thread which checks for updates/synchronize at a certain interval") isn't practical unless you are willing to abuse other backgrounding modes (which is likely to fail AppStore review process). 因此,你的第二个选项(“让应用程序在后台运行/有一个后台线程检查更新/以某个时间间隔同步”)是不切实际的,除非你愿意滥用其他后台模式(这可能会使AppStore失败)审查过程)。

Starting from iOS 7 it became possible to send "silent" push notifications (ie ones having content-available set), eg notifications that will be delivered directly to the app (instead of displaying alert and only launching app only when user confirms it). 从iOS 7开始,可以发送“静音”推送通知(即具有content-available设置的推送通知),例如将直接发送到应用程序的通知(而不是仅在用户确认时显示警报和仅启动应用程序)。 This makes important difference for backgrounding and is quite convenient actually. 这对背景化起了重要作用,实际上非常方便。 Whenever your app receives such push notification, it can check for new data, sync data, etc (you get around 30 seconds of runtime). 每当您的应用收到此类推送通知时,它都可以检查新数据,同步数据等(运行时间大约为30秒)。 Apple claims to throttle "silent" push notifications, but not much details on this. 苹果声称要限制“无声”推送通知,但没有太多细节。 In my practice sending them every 15 minutes seem to work. 在我的实践中,每15分钟发送一次似乎有效。

You may argue that 30 seconds is way to little to sync data. 您可能会争辩说30秒是很难同步数据的方法。 That's true. 确实如此。 To solve this iOS 7 introduced background downloads and uploads that you can initiate via NSURLSession configured with [NSURLSessionConfiguration backgroundSessionConfiguration:] . 为了解决这个问题的iOS 7中引入的背景下载和上传,您可以通过启动NSURLSession与配置[NSURLSessionConfiguration backgroundSessionConfiguration:] Download/upload will then be handled by the system and once it completes your app will be woken to handle it. 下载/上传将由系统处理,一旦完成,您的应用程序将被唤醒处理它。

To the best of my knowledge this is the best approach you can take for regular data synchronisation from background apps. 据我所知,这是您从后台应用程序进行常规数据同步的最佳方法。 This won't work for iOS 6, though. 但这不适用于iOS 6。

Alternatively, if your server can decide if new data is available and if user should be alerted, traditional push notifications should be sufficient. 或者,如果您的服务器可以决定是否有新数据可用以及是否应该提醒用户,则传统的推送通知应该足够了。 In this case there's no need for your app to run in background and check for new data (server can do this and send remote notification with an alert/sound). 在这种情况下,您的应用程序无需在后台运行并检查新数据(服务器可以执行此操作并发送带有警报/声音的远程通知)。 This will obviously work on iOS 6, too. 这显然也适用于iOS 6。

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

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