本地横幅通知终止应用程序

我想在用户退出应用程序时显示标题通知。 点击该横幅我想要打开我的应用程序。

func showBanner() { UIApplication.shared.cancelAllLocalNotifications() let notif = UILocalNotification.init() notif.alertBody = "Your Message Here..." localNotif.soundName = UILocalNotificationDefaultSoundName UIApplication.shared.presentLocalNotificationNow(notif) } 

如果我把这个代码在applicationDidEnterBackground它工作正常; 但是,当我把它放在applicationWillTerminate ,它不会。

任何解决方法?

编辑:

添加fireDate ,如下所示:

 var dc = DateComponents() dc.second = 2 // 2 seconds from current date time notif.fireDate = Calendar.current.date(byAdding: dc, to: Date()) 

还是行不通。 任何想法?

当我在应用程序中使用CoreBluetooth进行通信时,相同的代码适用于info.plist中的“ Requires Background Modes

得到了本答案的提示

我们希望,苹果不会拒绝我的应用程序。

不保证applicationWillTerminate永远不会被调用可能是reasone你的代码不工作。 所以如果你需要做任何事情之前,应用程序存在然后调用applicationDidEnterBackground

更多检查这个iOS – 如何保证applicationWillTerminate将被执行