问题
I want to show banner notification when user quits the Application. And on tapping that banner I want my Application to open.
func showBanner() {
UIApplication.shared.cancelAllLocalNotifications()
let notif = UILocalNotification.init()
notif.alertBody = "Your Message Here..."
localNotif.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.presentLocalNotificationNow(notif)
}
If I put this code in applicationDidEnterBackground
it works fine; But, when I put it in applicationWillTerminate
, it doesn't.
Any workaround?
EDIT:
Added fireDate
like following:
var dc = DateComponents()
dc.second = 2 // 2 seconds from current date time
notif.fireDate = Calendar.current.date(byAdding: dc, to: Date())
Still not working. Any idea?
回答1:
The same code works when I set in App communicates using CoreBluetooth in info.plist
for Requires Background Modes
.
Got the hint from This Answer
Let's hope, Apple does not reject my App.
回答2:
no guarantee that applicationWillTerminate
will ever get called may that is the reasone your code not working . so if your need to do anything before app exist then call applicationDidEnterBackground
more check this iOS - How to guarantee that applicationWillTerminate will be executed
来源:https://stackoverflow.com/questions/42367803/local-banner-notification-for-terminating-app