ios - 让App在收到UILocalNotification时执行后台代码

标签 ios objective-c iphone background notifications

我正在使用 iOS 8,并且我正在安排一个 UILocalNotification 作为解决方法,以解决我无法执行后台代码的事实,除非在有限的时间内或该应用程序属于某些类别(例如,与外部 BLE 配件通信等。但即便如此,它也将仅限于该代码):

Background execution documentation here ):

If your app is in the middle of a task and needs a little extra time to complete that task, it can call the beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method of the UIApplication object to request some additional execution time.

我想要实现的是检查在触发 UILocalNotification 时是否满足某些条件(使用本地通知似乎是让应用执行某些操作的唯一方法在后台时..)。 除非有另一种方式..

不幸的是,我似乎只能在应用程序进入前台时拦截调用并执行代码(用户点击通知标志并调用 ** didReceiveLocalNotification**。

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif

这对应于 Apple here 描述的行为.

The user taps the default button in the alert or taps (or clicks) the app icon. If the default action button is tapped (on a device running iOS), the system launches the app and the app calls its delegate’s application:didFinishLaunchingWithOptions: method, passing in the notification payload (for remote notifications) or the local-notification object (for local notifications). Although application:didFinishLaunchingWithOptions: isn’t the best place to handle the notification, getting the payload at this point gives you the opportunity to start the update process before your handler method is called.

关于如何在应用程序处于后台时拦截 UILocalNotification 并执行一些代码而无需用户点击通知的想法?

最佳答案

您不能使用本地通知本身的触发来在后台启动您的应用程序。正如您所注意到的,您的应用程序仅在用户点击通知时才会执行。

您可以使用带有内容可用 key 的远程通知在后台启动您的应用程序。

关于ios - 让App在收到UILocalNotification时执行后台代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31382187/

相关文章:

ios - ImageView 未显示,但选择确实有效

ios - 在运行时和方向更改期间居中和sizeToFit subview

ios - UICollectionViewLayout 何时调用 layoutAttributesForItemAtIndexPath

ios - sortedArrayUsingSelector 警告

objective-c - 内存管理和异步操作: when does an object become nil?

iphone - iPhone 上 Core Data 中的这些数据类型 int 16、int 32 和 int 64 到底是什么?

ios - 无法为 UIBarButtonItem 设置图像

ios - 如何在 UITableView 滚动时缩小 UIImageView 内容(图像)?

iphone - 该应用程序在 iOS 4.1+ 中崩溃,但在 4.0 中成功运行

ios - 如何找到链接到stdlibc++的依赖项?