簡體   English   中英

如果在application:continueUserActivity:restorationHandler中調用,NSUserNotification僅顯示一次

[英]NSUserNotification only display once if invoked in application:continueUserActivity:restorationHandler

我正在針對macO開發一個簡單的Spotlight擴展。 我的應用程序會將一些內容編入索引。 當我輸入一些關鍵字時,spotlight將觸發應用程序的application:continueUserActivity:restorationHandler 並且application()將發送本地通知。 代碼如下:

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)
}

我使用macOs 10.14.3

如果我的應用程序未運行,並且我在Spotlight中鍵入關鍵字,則Spotlight將啟動我的應用程序並運行application() 而且我會正確看到通知。

但是,如果我再次鍵入關鍵字(我的應用程序正在后台運行),則聚光燈仍然會觸發application()方法,但不會出現通知。 更糟糕的是,我可以在通知中心看到通知。

有人可以幫忙嗎? 謝謝。

要單獨顯示,每個通知必須具有唯一的標識符。

notification.identifier = "different_kind"

從文檔(重點是我的):

標識符對於通知是唯一的。 使用與現有通知相同的標識符傳遞的通知將替換現有通知,而不是導致顯示新通知。

關於發布新通知的速度也有限制,因此請注意。

最后,如果您正在開發Mojave,則您可能應該考慮使用UNUserNotificationCenter ,因為NSUserNotificationCenter已過時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM