我正在使用远程通知来唤醒我的应用并有条件地触发 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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |