iOS UILocalNotification 仅在应用未启动时发出声音(无警报)运行

iOS UILocalNotification only makes a sound (no alert) when app is not running

我正在使用远程通知来唤醒我的应用程序并有条件地触发 UILocalNotification。

我发现如果在应用程序未运行时触发此通知 运行,我得到的只是通知声音(没有 alert/notification)。如果应用程序在后台,它会按预期工作。

正在 AppDelegate 中注册远程通知:

let settings = UIUserNotificationSettings(forTypes: (.Badge | .Sound | .Alert), categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
UILocalNotification 的

Creating/Firing(didReceiveRemoteNotification 的结果)

var notification = UILocalNotification()
notification.alertTitle = "My App"
notification.alertBody = "Hello World!"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().presentLocalNotificationNow(notification)

据我从文档中得知,应用程序在后台时的通知行为与不在后台时的通知行为 运行 应该是相同的..

When the system delivers a local notification, several things can happen, depending on the app state and the type of notification. If the app is not frontmost and visible, the system displays the alert message, badges the app, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the app is woken up or launched.

来源:UILocalNotification Documentation

非常感谢深入了解这里发生的事情!

该问题与本地通知的显示无关

正如您在问题中所说,您使用远程通知来触发打开您的应用程序,然后使用 运行 代码有条件地显示本地通知。

这不起作用的原因是:

您显示本地通知的代码 不是 运行 在用户强行关闭应用程序(通过向上滑动)后来自远程通知!

您可以在此处的开发者论坛中找到更多相关信息: https://devforums.apple.com/message/873265#873265(需要开发帐户登录)

Also keep in mind that if you kill your app from the app switcher (i.e. swiping up to kill the app) then the OS will never relaunch the app regardless of push notification or background fetch. In this case the user has to manually relaunch the app once and then from that point forward the background activities will be invoked.