【问题标题】:Notification does not trigger userNotificationCenter on macOS Big Sur 11.6通知不会触发 macOS Big Sur 11.6 上的 userNotificationCenter
【发布时间】:2021-12-08 16:19:07
【问题描述】:

当我使用终止参数运行应用程序时(使用“Product”/“Scheme”/“Edit Scheme...”/“Run”/“Arguments”/“Argument Passes On Launch”设置),macOS 通知中心会出现一条通知并且应用程序终止。

#testapp application did finish launching
#testapp terminate mode enabled
#testapp terminating…

到目前为止,一切都很好......预期。

当我点击通知时,应用程序启动但userNotificationCenter 未触发(我在控制台应用程序中看不到#testapp notification triggered,但我看到以下内容)。

#testapp application did finish launching
#testapp terminating…

不正常吧?我该如何解决这个问题?

我开始认为这是 11.6 版中的 Big Sur 错误。

Big Sur 版本 11.4 (M1) 和 11.5 (Intel) 上一切正常。

感谢您的帮助!

//
//  AppDelegate.swift
//  Test
//
//  Created by Sun Knudsen on 2021-10-22.
//

import Cocoa
import UserNotifications

@main
class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {
  func showNotification(){
    let content = UNMutableNotificationContent()
    content.body = "Hello"
    let request = UNNotificationRequest(
      identifier: UUID().uuidString,
      content: content,
      trigger: nil
    )
    UNUserNotificationCenter.current().add(request)
  }
  
  func terminate() -> Void {
    DispatchQueue.main.async {
      NSApp.terminate(self)
    }
  }
  
  func applicationDidFinishLaunching(_ notification: Notification) {
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (allowed, error) in
      NSLog("#testapp application did finish launching")
      if CommandLine.arguments.indices.contains(1) && CommandLine.arguments[1] == "terminate" {
        NSLog("#testapp terminate mode enabled")
        self.showNotification()
        self.terminate()
      } else {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
          self.terminate()
        }
      }
    }
  }

  func applicationWillTerminate(_ aNotification: Notification) {
    NSLog("#testapp terminating…")
  }
  
  func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
  ) {
    NSLog("#testapp notification triggered")
    self.terminate()
    completionHandler()
  }
}

在 GitHub 上https://github.com/sunknudsen/test-app 上提供测试应用程序。

【问题讨论】:

标签: swift macos cocoa appkit apple-m1


【解决方案1】:

此问题是由 macOS Big Sur 11.6 中的错误引起的。

在 macOS Big Sur 11.6.1 或 macOS Monterey 中一切正常。

【讨论】:

    猜你喜欢
    • 2021-03-16
    • 2021-11-24
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 2022-01-09
    • 2021-06-19
    • 2021-08-04
    • 2021-05-31
    相关资源
    最近更新 更多