gpt4 book ai didi

swift - 使用 XCode 进行测试时,MacOS 应用程序本地通知不显示

转载 作者:行者123 更新时间:2023-12-04 01:30:13 26 4
gpt4 key购买 nike

我试图向我的 macOS swift 应用程序添加横幅通知生成器,但在 XCode 中测试运行时横幅没有出现,并且通知中心也没有任何新通知可见。我电脑上的其他应用程序会定期生成通知。我错过了什么?我已在请求时授予许可

我的应用委托(delegate)如下

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

请帮我

最佳答案

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

因此,我将 generateNotification 函数替换为以下内容,并且一切正常。

let notificationCenter = UNUserNotificationCenter.current();
notificationCenter.getNotificationSettings
{ (settings) in
if settings.authorizationStatus == .authorized
{
//print ("Notifications Still Allowed");
// build the banner
let content = UNMutableNotificationContent();
content.title = summary ;
content.body = title ;
if sound == "YES" {content.sound = UNNotificationSound.default};
// could add .badge
// could add .userInfo

// define when banner will appear - this is set to 1 second - note you cannot set this to zero
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false);

// Create the request
let uuidString = UUID().uuidString ;
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger);

// Schedule the request with the system.
notificationCenter.add(request, withCompletionHandler:
{ (error) in
if error != nil
{
// Something went wrong
}
})
//print ("Notification Generated");
}

关于swift - 使用 XCode 进行测试时,MacOS 应用程序本地通知不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61185198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com