Я попытался добавить генератор уведомлений о баннерах в мое приложение MacOS Swift, и баннер не отображается при тестовом запуске в XCode, и в центре уведомлений не отображаются новые уведомления. Другие приложения на моем компьютере регулярно генерируют уведомления. Что я пропустил? Я получил разрешение при запросе
Мой делегат приложения выглядит следующим образом
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
@IBOutlet weak var mainMenu: NSMenu!
func applicationDidFinishLaunching(_ aNotification: Notification)
{
NSUserNotificationCenter.default.delegate = self ;
}
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool
{
return true
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
При запуске приложения я запускаю следующий метод и вижу строку консоли «Уведомления разрешены»
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge, .provisional])
{ granted, error in
if error != nil
{
print ("Request notifications permission Error");
};
if granted
{
self.allowNotifications = true ;
print ("Notifications allowed");
}
else
{
self.allowNotifications = false ;
print ("Notifications denied");
};
}
Метод, который я добавил в свой ViewController, выглядит следующим образом, и я проверил, что оператор print в конце достигнут
func generateNotification (summary:String, sound:String, title:String , body:String)
{
let notification = NSUserNotification()
if !allowNotifications {return};
notification.title = summary ;
notification.subtitle = title ;
notification.informativeText = body ;
if (sound == "YES") {notification.soundName = NSUserNotificationDefaultSoundName};
NSUserNotificationCenter.default.deliver (notification);
print ("notification generated");
};
Пожалуйста, помогите мне