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

标签 ios apple-push-notifications uilocalnotification

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

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

在 AppDelegate 中注册远程通知:

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

创建/触发 UILocalNotification(作为 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.

关于iOS UILocalNotification 仅在应用程序未运行时发出声音(无警报),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30948864/

相关文章:

ios - 每天不同时间本地通知

ios - opengl 应用程序从套接字连接监听/接收数据?

ios - 按钮中的序列事件

ios - Apple Push Notifications - 应用商店提交

ios - 如何防止 ios 中的本地通知重复?

ios 检查显示的本地通知

ios - 后退按钮项替换为 "Back"

ios - 显示另一个 ViewController 的最有效方式

ios - 当应用程序在后台运行时,将通知从 Web 服务器拉取到 iOS 设备

ios - flutter IOS : CocoaPods could not find compatible versions for pod "simple_permissions":