Local banner notification for terminating App

混江龙づ霸主 提交于 2019-12-24 10:53:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!