NSUserNotification only display once if invoked in application:continueUserActivity:restorationHandler

157 Views Asked by At

I am developing a simple spotlight extension on macOs. My app will index some content into spotlight. When I type some keywords, spotlight will trigger app's application:continueUserActivity:restorationHandler. And the application() will send a local notification. The code looks like this:

func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler: ([NSUserActivityRestoring]) -> Void) -> Bool {
    let notification = NSUserNotification()

    notification.title = "title"
    notification.subtitle = "subtitle"
    notification.soundName = NSUserNotificationDefaultSoundName

    NSUserNotificationCenter.default
        .deliver(notification)
}

And I use macOs 10.14.3.

If my app does not running, and I type the keyword in spotlight, the spotlight will start my app and run the application(). And I will see the notification correctly.

However, if I type keyword again(my app is running background), the spotlight still tigger the application() method but the notification does not appeared. Even worse, I could see the notification in the notification center.

Anyone can make help? Thanks.

1

There are 1 best solutions below

0
James Bucanek On

To be displayed separately, each notification mush have a unique identifier.

notification.identifier = "different_kind"

From the documentation (emphasis mine):

The identifier is unique to a notification. A notification delivered with the same identifier as an existing notification replaces the existing notification rather than causing the display of a new notification.

There are also limitations on how quickly you can post new notifications, so be aware of that.

Finally, if your'e developing for Mojave, you should probably be looking into UNUserNotificationCenter, as NSUserNotificationCenter is deprecated.