使用 XCode 测试时未显示 MacOS 应用程序本地通知 [英] MacOS App Local Notification Not Showing when testing with XCode

查看:75
本文介绍了使用 XCode 测试时未显示 MacOS 应用程序本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have tried to add a banner notification generator to my macOS swift app and the banner does not appear when test running in XCode and neither are there any new notifications visible in the notification centre. Other apps on my computer are generating notifications regularly. What have I missed? I have granted permission when requested

我的应用委托如下

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
    }

在应用程序启动时,我运行以下方法,我看到控制台行允许通知"

On app startup I run the following method and I see the console line "Notifications allowed"

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语句

The method I have added to my ViewController is as follows and I have tested that the print statement at the end is reached

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 中也是如此.)

I believe that my problem here was asking permission to use UNUserNotification and then using NSUserNotification to create the notification itself, which of course I had not requested permission to use. Requesting permission is now mandatory in Catalina (and perhaps it was in earlier versions of macOS as well.)

所以我用以下代码替换了 generateNotification 函数,并且一切正常.

So I replaced the generateNotification function with the following and it all works correctly.

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

这篇关于使用 XCode 测试时未显示 MacOS 应用程序本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆