1

I have a database where products with expiration dates are stored. When the expiry date expires, the user should receive a notification about this. But these notifications should also come if the user turned off their application (kill app). Question: how to do this? Show me the code, please. My code is (AppDelegate):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in }

    return true
}

and (ViewController):

    let center = UNUserNotificationCenter.current()

    let content = UNMutableNotificationContent()
    content.title = "title"
    content.body = "\(overdueProductsStringArray)"
    content.sound = UNNotificationSound.default()

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "t", content: content, trigger: trigger)

    center.add(request, withCompletionHandler: nil)
Tkas
  • 302
  • 3
  • 14
  • Please refer https://stackoverflow.com/questions/27732784/local-notification-while-app-not-running – Vipul Sep 22 '18 at 06:34
  • @Vipul thank you, but your code is iOS < 9. My code is iOS 10. Is my code similar to code from link? So, is my code working? – Tkas Sep 22 '18 at 06:43
  • @Tkas https://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3/45247943#45247943 – Vipul Sep 22 '18 at 06:51
  • @Vipul thank you for your help to me! But it code for app when it app in foreground – Tkas Sep 22 '18 at 06:54

2 Answers2

3

If you go to AppDelegate there is a method called applicationWillTerminate so you just need to call your method that send locations.

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    let viewControllerWithYourFunction = ViewControllerWithYourFunction()
    viewControllerWithYourFunction.sendLocation()
}
Kerberos
  • 4,036
  • 3
  • 36
  • 55
  • I've edited the answer by replacing `function` with `method`. A method is a function but it is part of a class, like in this case. You can learn more [here](https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function), greetings from Lanciano. – Kerberos Sep 27 '18 at 20:20
0

didFinishWithLaunchOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

     if let launchOptions = launchOptions,
            let userInfo =  launchOptions[.remoteNotification] as? [AnyHashable: Any] {
            //some code here and post when needed like below
            NotificationCenter.default.post(name: NSNotification.Name("your notification name"),  object: nil)
        }
}
Napster Scofield
  • 1,271
  • 14
  • 13