1

i'm trying to send a local notification following this tutorial: https://useyourloaf.com/blog/local-notifications-with-ios-10/

I added this code to my viewDidLoad():

    let center = UNUserNotificationCenter.current()
    let options: UNAuthorizationOptions = [.alert, .sound];
    center.requestAuthorization(options: options) {
        (granted, error) in
        if !granted {
            print("Something went wrong")
        }
    }
    let content = UNMutableNotificationContent()
    content.title = "Don't forget"
    content.body = "Buy some milk"
    content.sound = UNNotificationSound.default()
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5,
                                                    repeats: false)

    let identifier = "UYLLocalNotification"
    let request = UNNotificationRequest(identifier: identifier,
                                        content: content, trigger: trigger)
    center.add(request, withCompletionHandler: { (error) in
        if let error = error {
            print("error")
        }
    })

but 5 seconds pass without any notification shown. the CompletionHandler is called with error being nil

Ginso
  • 459
  • 17
  • 33
  • And when it should fire, your app is on foreground or background? Check https://stackoverflow.com/questions/49902505/local-notification-views-to-appear-in-foreground/49937138 ? – Larme Jun 28 '18 at 10:16
  • i want it in both cases – Ginso Jun 28 '18 at 10:24
  • In background, that's should be normal. In foreground you need to follow the link. Also, to make better tests, launch it in 30 seconds at least. – Larme Jun 28 '18 at 10:27
  • ok i just notized that it actually does work when the app is in background. As for the link, i don't understand what they are suggesting and im not familiar with objective-c – Ginso Jun 28 '18 at 10:30
  • https://stackoverflow.com/questions/39868193/how-to-use-unnotificationpresentationoptions ? – Larme Jun 28 '18 at 10:31
  • sry, i don't get it. The link shows me an extension but not how to use it. I also don't see, waht this extension does – Ginso Jun 28 '18 at 10:52

1 Answers1

0

If you want the notification when app is in foreground, then you'll have to add some more code into your controller,

  • Add the following line at the end of viewDidLoad()

    center.delegate = self

    This makes your ViewController the delegate to which the notification can callback when it hits.

  • Confirm your view controller to the delegate, (Add UNUserNotificationCenterDelegate)

    class ViewController: UIViewController, UNUserNotificationCenterDelegate {

    Now you can write callback methods in your ViewController.

  • Write the callback to present your notification, you can add this method right after viewDidLoad()

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .badge, .sound]) }

    This will make your notification to popup even if the app is in foreground.

NOTE: It is highly recommended that you register your notifications (Local/Remote) in AppDelegate rather than ViewController.

Aju Antony
  • 639
  • 6
  • 13
  • thanks, it worked. Just for my understanding: i don't get why this func has any effect, who calls it? – Ginso Jun 28 '18 at 11:33
  • The default behaviour of the notification is to ignore it if the app is in foreground. because it is expected that if needed, the developer will provide what is to be done when the notification is received in foreground. Some developers want to show a stylish alert or want some action to be executed. Regardless if you call the `completionHandler`, the app processes that you'll need the `.alert` which shows the banner, `.badge` to update the badge count if available and, `.sound` to play the sound. – Aju Antony Jun 28 '18 at 12:22