我想获取新事件的列表,因此我需要在事件附近不断调用服务。然后只有我可以通过 API 调用发送我的 uid 和 eventide,然后后端团队通过事件通知你在附近。 所以这个服务如何在应用程序终止/终止/暂停时调用,因为这些东西在应用程序在后台运行时工作。
最佳答案
如果您的应用程序流程可行,您可以遵循静默通知方式,每当您需要调用方法时,您可以向设备发送静默通知,
- 设备获得听起来不像推送通知的无声通知,
- 您的应用将在后台唤醒,并在那时
- 您可以调用您的方法。
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.
}
关于ios - 如何在应用程序快速终止/终止/暂停时调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59089247/