【问题标题】:How to call a method while app is kill/terminate/suspended in swift如何在应用程序快速终止/终止/暂停时调用方法
【发布时间】:2019-11-28 21:04:06
【问题描述】:

我想获取新事件的列表,因此我需要在事件附近时不断调用服务。然后只有我可以通过 API 调用发送我的 uid 和 eventide,然后后端团队通知你在事件附近。 因此,此服务如何在应用程序终止/终止/暂停时调用,因为这些东西在应用程序在后台运行时工作。

【问题讨论】:

  • 如果应用程序被终止,您几乎无能为力。您最好的选择是在有更新的事件详细信息时使用重要的位置变化监控和来自您的 bs 借出的推送通知。从电池或网络使用的角度来看,轮询效率不高。

标签: ios swift


【解决方案1】:

如果在您的应用流程中可行,您可以采用静默通知方式,每当您需要调用某个方法时,您都可以向设备发送静默通知,

  • 设备收到静音通知,听起来不像推送通知,
  • 您的应用将在后台唤醒,此时,
  • 你可以调用你的方法。
func registerForNotification() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            guard error == nil else { return }
            if granted {
                //Register for RemoteNotifications. Your Remote Notifications can display alerts now :)
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
            else {
                //Handle user denying permissions..
            }
        }
        providerDelegate = ProviderDelegate()
        pushRegistry.delegate = self
        pushRegistry.desiredPushTypes = [.voIP]
    }
func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType) {
//Here you get the token, send it to server.
}

【讨论】:

  • 请记住,这是一种尽力而为的功能。如果用户关闭了 bg refresh,电池几乎没电了,或者由于任何其他原因,iOS 可以决定延迟或省略向您的应用程序发送通知。根据 Apple 的文档,您也不应该每小时发送超过 2-3 个通知。 developer.apple.com/documentation/usernotifications/…
  • 您不能使用 voip 推送,除非您要指示有传入的 voip 呼叫。如果您的应用未能在 iOS 13 中使用 CallKit 指示传入的 voip 呼叫,它将被终止并且不会收到任何进一步的推送通知。您可以使用普通的静默推送通知,但如果用户终止了您的应用,这些通知将不会执行。
  • @Paulw11 我们可以为这个任务使用其他推送类型吗?
  • 没有其他推送类型可以重新启动已终止的应用程序
猜你喜欢
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
相关资源
最近更新 更多