我们一直在使用 Swift 开发 iOS 应用程序(仅限 iOS 9+)。我们对某些事情使用 VOIP 通知,然后使用本地通知向用户实际显示消息。
我的问题是,当手机锁定时,即使我可以看到调试输出并且代码在我期望的时候立即运行,本地通知也可能需要长达 15 秒的时间才能真正显示。
这是我用于显示通知的代码:
let notification = UILocalNotification()
notification.alertTitle = "Title text.".local
notification.alertBody = "Body text."
notification.alertAction = "Action".local
notification.category = Notification.CallCategory
notification.soundName = localNotificationSoundName
notification.userInfo = msg.dictionary()
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
除了延迟之外,一切都按预期工作,当手机未锁定时,没有延迟,通知按预期工作。
有什么想法吗?
编辑
作为澄清,此代码在驻留在我们的 PKPushRegistryDelegate 实现中的 switch/case 内运行。具体来说,在这个函数中:
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!)
此外值得注意的是,这种情况并非在所有设备上都会发生。我们有一台 iPhone 6s 测试设备,每次都会发生这种情况,无论我们重启设备、重新安装应用程序甚至将 iOS 升级到新版本多少次。我们还有一些其他测试设备,但没有发生这种情况。
最佳答案
从 Apple 文档中阅读此内容 -
Prioritize Remote Notification Delivery
Remote notifications provided by your server to the Apple Notification Service include a variety of elements, including payload data, an expiration date, a priority, and more. Remote notifications support two levels of push priority. One delivers the notification immediately. The other delays delivery of the notification until an energy-efficient time. Unless a notification truly requires an immediate delivery, use the deferred delivery method.
我知道您的问题是
本地通知
速度慢,但仍请确保您已优先考虑远程通知。我在测试时发现应用程序在使用本地通知时有一定的延迟。也许这就是系统的设计方式,并不一定是缺陷。对于 VOIP 情况,我仍然认为您应该使用推送通知来通知调用(也许您在发出通知之前进行了一些内部计算)。但请检查一下 Facebook Messenger 应用程序,它看起来确实混合了匹配推送/本地通知来通知用户有来电。尝试观察延迟(如果有)。
制作另一个简单的应用程序,显示与上面相同的代码的本地通知,并观察它是否也有一些延迟。对于此测试,不要使用远程通知来触发本地通知,只需在应用程序启动或类似的操作后进行测试。这是为了确保系统是否在推送通知后立即延迟某些应用程序的通知?
关于iOS:手机锁定时本地通知显示速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37683856/