简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM