简体   繁体   English

iOS:手机锁定时显示本地通知的速度很慢

[英]iOS: Local notification very slow to show up when phone locked

We've been developing an iOS application (iOS 9+ only) in Swift.我们一直在用 Swift 开发 iOS 应用程序(仅限 iOS 9+)。 We are using VOIP notifications for certain things then use local notifications to actually show the message to the user.我们对某些事情使用 VOIP 通知,然后使用本地通知向用户实际显示消息。

My problem is that when the phone is locked it can take up to 15 seconds for the local notification to actually get displayed, even though I can see my debug output and the code being ran immediately when I expect it.我的问题是,当手机被锁定时,实际显示本地通知可能需要长达 15 秒的时间,即使我可以看到我的调试输出并且代码在我期望的时候立即运行。

This is my code for showing the notification:这是我显示通知的代码:

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)

Other than the delay everything works as expected and when the phone is not locked there is no delay and the notification works as expected.除了延迟,一切都按预期工作,当手机没有锁定时,没有延迟,通知按预期工作。

Any ideas?有任何想法吗?

EDIT编辑

As a clarification this code is being run inside a switch/case that resides in our implementation of PKPushRegistryDelegate.作为澄清,此代码在驻留在我们的 PKPushRegistryDelegate 实现中的 switch/case 中运行。 Specifically within this function:特别是在这个函数中:

func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!)

Additionally it is worth noting that this not happen on every device.此外值得注意的是,这并非在每个设备上都会发生。 We have one iPhone 6s test device where it happens every time, regardless of how many times we reboot the device, reinstall the app or even upgrade iOS to a newer version.我们有一台 iPhone 6s 测试设备,无论我们重启设备多少次,重新安装应用程序,甚至将 iOS 升级到更新版本,它每次都会发生。 We have a few other test devices where it doesnt happen.我们还有其他一些测试设备没有发生这种情况。

Read this from Apple doc -从 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.您的服务器向 Apple 通知服务提供的远程通知包括各种元素,包括有效负载数据、到期日期、优先级等。 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.除非通知确实需要立即传递,否则请使用延迟传递方法。

NotificationBestPractices 通知最佳实践

  1. I know your problem is for slow local notification but still make sure you have Prioritize remote notification.我知道您的问题是local notification缓慢,但仍要确保您有优先远程通知。

  2. I have experienced while testing that app has certain delay when using local notifications.我在测试该应用程序时使用本地通知时有一定的延迟。 Maybe that is how system is designed and not necessarily a flaw.也许这就是系统的设计方式,不一定是缺陷。 For VOIP case I still think you should have used push notification to notify the call (Maybe you have some internal calculation before making notification).对于 VOIP 情况,我仍然认为您应该使用推送通知来通知呼叫(也许您在发出通知之前进行了一些内部计算)。 But do check out Facebook messenger app, it looks like it does mix match push / local notification to notify user for calls.但是请查看 Facebook Messenger 应用程序,它看起来确实混合了推送/本地通知以通知用户来电。 Try to observe the delay if any.尝试观察延迟(如果有)。

  3. Make another simple app which shows Local Notification for same code as above and observe if it too has some delay.制作另一个简单的应用程序,它显示与上述相同代码的本地通知,并观察它是否也有一些延迟。 For this test don't use remote notification to fire the Local Notification, just test after app launch or something like that.对于此测试,不要使用远程通知来触发本地通知,只需在应用程序启动或类似的情况下进行测试。 This is to make sure if system is delaying certain apps notification for case immediately after push notification ?这是为了确保系统是否在推送通知后立即延迟某些应用程序通知?

请尝试设置->常规->重置->重置网络设置

I found the possible cause - iPhone was paired with Apple Watch.我找到了可能的原因 - iPhone 与 Apple Watch 配对。 In spite of the fact that the watch was in the drawer and turned off, iPhone was trying to route the notifications to it.尽管手表在抽屉里并处于关机状态,但 iPhone 仍试图将通知发送给它。 After we had unpaired iPhone from watch and wait for a while, everything became normal.在我们从手表上取消配对 iPhone 并等待一段时间后,一切都变得正常了。

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

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