我不确定这是不可能的,或者如果我错误地实现它。处于非活动应用程序状态的本地通知
我试图完成使用beginBackgroundTask
(expirationHandler
),然后张贴LocalNotification
一旦完成对iOS10
在后台任务。问题在于Notification
不会一直触发 - 我可以设置breakpoint
以使用presentLocalNotificationNow
来查看它,但它并不总是显示给用户,并且只有当应用程序返回到(通过从主屏幕打开它) didReceiveLocalNotification
被调用。
如果我在调度通知时检查应用程序状态,它的rawValue是2,我认为它是不活动的,所以我假设这是它没有向用户传递通知的原因。
我已经尝试在任务完成后安排notification
5秒,然后立即调用endBackgroundTask
,但通知仍然不会一直显示给用户。 EndBackgroundTask
不会被调用,我认为应该将应用程序从非活动状态移动到notification
应该显示的背景,但我假设它不是立即执行,并且在实际发生时由系统自行决定。
我在做什么错?
相关的代码是:
let task = UIApplication.shared.beginBackgroundTask(expirationHandler: nil)
// Long running task
let notification = UILocalNotification()
notification.soundName = UILocalNotificationDefaultSoundName
notification.alertBody = "Message"
let settings = UIUserNotificationSettings(types: [.alert, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.presentLocalNotificationNow(notification)
UIApplication.shared.endBackgroundTask(task)