5

When I first run my app the simulator is able to display notifications whilst the app is in the foreground.

When I relaunch the app didReceive notification: (i.e. notification method from iOS 9) is called instead of willPresent notification (i.e. notification method from iOS 10) and no notifications are displayed whilst the app is in the foreground. However, a notification is displayed if the app is in the background.

Neither this question or this question provided a solution to this problem.

I get authorisation for notifications using this code:

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]? = [:]) -> Bool {
                if #available(iOS 10.0, *) {
                    let center = UNUserNotificationCenter.current() 
                    center.requestAuthorization(options: [.alert, .sound,  .sound]) { (granted, error) in              
                    UNUserNotificationCenter.current().delegate = self                   
                    }                      
                }
                return true
            }

And I've implemented the willPresent notification method:

@available(iOS 10.0, *)
            func userNotificationCenter(_ center: UNUserNotificationCenter,
                                        willPresent notification: UNNotification,
                                        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)                     completionHandler(UNNotificationPresentationOptions.alert)
            }

Here's how I create the notification:

let content = UNMutableNotificationContent()
        content.title = title
        content.subtitle = subtitle
        content.body = message
        content.categoryIdentifier = identifier                      

        let trigger = UNTimeIntervalNotificationTrigger(
            timeInterval: 10.0,
            repeats: false)

        let request = UNNotificationRequest(
            identifier: "identifier",
            content: content,
            trigger: trigger
        )

        UNUserNotificationCenter.current().add(
            request, withCompletionHandler: nil)

Problem in a nutshell: How do I ensure the app is able to display a notification when the app is in the foreground after the first time that the app is run?

Thanks in advance.

Community
  • 1
  • 1
Micah Simmons
  • 2,078
  • 2
  • 20
  • 38
  • It is because the identifier is same for all the notifications. make sure you add different identifiers for different notifications. Also, why are you asking for `.sound` two times in permission? – bhakti123 Feb 02 '17 at 13:50

1 Answers1

3

Try setting the delegate outside of the requestAuthorization completion block. It's probably happening later than you want.

I added a sample project here: https://github.com/jerbeers/DemoLocalNotification

If you build and run, tap the button, you'll see the local notification 3 seconds later. If you stop, build and run again, I see the local notification even while the app is running.

Jerry
  • 4,382
  • 2
  • 35
  • 29
  • Thanks for your response. I tried setting the delegate both before and after the requestAuthorization completion block and it didn't solve the issue. Can you suggest any other solutions? – Micah Simmons Oct 28 '16 at 23:54
  • Tell me the steps you're taking. When you say "first run" and "relaunch", what specifically are you doing? – Jerry Oct 29 '16 at 03:49
  • By first run I mean; the first time I `install` the app, and by `relaunch` I mean; when I reload the app via Xcode in the simulator after it has been installed. – Micah Simmons Oct 29 '16 at 17:54
  • How are you installing the app? From TestFlight or from Xcode, or what? – Jerry Oct 29 '16 at 17:58
  • I'm installing the app using Xcode, I'm not currently using TestFlight. – Micah Simmons Oct 29 '16 at 17:59
  • So, you build and run, tap a button or something, and the app shows local notifications. You stop in Xcode, build and run again, tap the button, and it doesn't? – Jerry Oct 29 '16 at 18:01
  • It's basically that scenario. In essence, I install the app, create a local notification and the notification displays whilst the app is in the foreground. Then when I relaunch the app without changing any code and create a local notification in the same way the local notification will only display once the app is no-longer in the foreground. – Micah Simmons Oct 29 '16 at 18:06