使用XCode测试时未显示MacOS App本地通知

问题描述 投票:1回答:1

我已经尝试将横幅广告通知生成器添加到我的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");
    };

请帮助我

swift macos notifications xcode8
1个回答
0
投票

[我相信我的问题是在请求使用UNUserNotification的权限,然后使用NSUserNotification本身创建通知,我当然没有请求使用权限。现在,在Catalina中强制要求许可(也许在早期版本的macOS中也是如此。)

© www.soinside.com 2019 - 2024. All rights reserved.