2016-10-07 124 views
1

我不确定这是不可能的,或者如果我错误地实现它。处于非活动应用程序状态的本地通知

我试图完成使用beginBackgroundTaskexpirationHandler),然后张贴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) 

回答

0

最好的我可以说,这个问题是由于presentLocalNotificationNow是异步和立即返回所以没有足够的时间向系统注册的通知。我通过提前安排通知来解决问题,所以有足够的时间。

相关问题