【发布时间】:2016-05-21 00:43:32
【问题描述】:
我正在尝试在我的应用程序被终止后触发本地通知,以警告用户无论应用程序在做什么,它都不能再这样做了。 MileIQ 会在后台位置跟踪中执行类似的操作,警告用户该应用已被终止,如果他们希望仍记录行程,则需要重新启动该应用。
我的代码在 UIApplicationWillTerminateNotification 处理程序中如下所示:
let notification = UILocalNotification()
notification.soundName = UILocalNotificationDefaultSoundName
notification.alertTitle = "title"
notification.alertBody = "body"
notification.fireDate = NSDate().dateByAddingTimeInterval(6)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
神奇的fireDate延迟设置为6秒是因为applicationWillTerminate有5 seconds to the app to cleanup,如果方法没有及时返回,iOS会杀死应用程序。
我注意到,如果我立即触发本地通知,它会非常短暂地出现,并在应用程序被 iOS 杀死时被关闭。通过添加 6 延迟,通知出现大约 50% 的时间并且不会被忽略。
我正在寻找的是一种可靠的解决方案,可以在应用程序被杀死之后显示这个本地通知。知道怎么做吗?
【问题讨论】:
-
你是如何杀死应用程序的。如果您使用应用切换器终止,applicationWillTerminate 将无法可靠执行
-
@Paulw11 该应用程序是从应用程序管理器手动终止的(双击主页按钮)。我不太关心操作系统会自行杀死应用程序。
标签: ios iphone swift notifications uilocalnotification