1

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?

sweta.me
  • 273
  • 1
  • 11

2 Answers2

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.

Community
  • 1
  • 1
sweta.me
  • 273
  • 1
  • 11
0

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

Community
  • 1
  • 1
Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73
  • But, I want the notification to appear only when the application is terminated. Not when it's in background. Any workaround? – sweta.me Feb 21 '17 at 12:57
  • How would I know? I don't even have that much reps to downvote. :D Btw, you can post the answer as comment to question, as it is not actually an answer. – sweta.me Feb 21 '17 at 13:02