1

When I run app using terminate argument (set using “Product“ / “Scheme” / “Edit Scheme…” / “Run” / “Arguments” / “Argument Passes On Launch”), a notification appears in macOS Notification Centre and app terminates.

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

So far, so good… Expected.

When I click notification, app launches but userNotificationCenter is not triggered (I don’t see #testapp notification triggered in Console app, but I see following).

#testapp application did finish launching
#testapp terminating…

Not normal right? How can I fix this?

I am starting to think that this is a Big Sur bug in version 11.6.

Everything works fine on Big Sur version 11.4 (M1) and 11.5 (Intel).

Thanks for helping out!

//
//  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()
  }
}

Test app available on GitHub at https://github.com/sunknudsen/test-app.

sunknudsen
  • 6,356
  • 3
  • 39
  • 76
  • You need to register notification category and notification actions to the system. – Mannopson Oct 22 '21 at 01:53
  • @Mannopson Given notifications work while app is running, would this fix issue when app is not? – sunknudsen Oct 22 '21 at 09:10
  • 1
    The cause of this issue might be the same as your other question [How can I detect if app was launched by user clicking notification on macOS now that launchUserNotificationUserInfoKey has been deprecated?](https://stackoverflow.com/questions/69660669/how-can-i-detect-if-app-was-launched-by-user-clicking-notification-on-macos-now). How is the notification scheduled? Post a [mre] please. – Willeke Oct 22 '21 at 10:15
  • Thanks for helping out @Willeke. Not sure how to post example, but the whole app is only ~150 lines. Please see https://github.com/sunknudsen/borg-wrapper/blob/master/Borg%20Wrapper/AppDelegate.swift. – sunknudsen Oct 22 '21 at 10:39
  • Btw, I am clearly missing a key piece of the puzzle. I am a senior web developer, but unfortunately, I really don’t get Swift. I have spend over 20 hours debugging this issue… somehow, feels like there might be a bug in macOS. This stuff should just work. But, again… perhaps I am missing a piece of the puzzle. – sunknudsen Oct 22 '21 at 10:41
  • How do you know if `userNotificationCenter(_:didReceive:withCompletionHandler:)` is triggered? – Willeke Oct 22 '21 at 11:22
  • @Willeke I added `NSLog("#borgwrapper triggered")` to `userNotificationCenter` and I track logs using Console app. – sunknudsen Oct 22 '21 at 11:44
  • 1
    TIL NSLog messages show up in Console.app (just tried it; they do) – Alexander Oct 22 '21 at 13:26
  • @Willeke I updated question with isolated example of issue. – sunknudsen Oct 22 '21 at 14:42

1 Answers1

0

This issue is caused by a bug in macOS Big Sur 11.6.

Everything works as expected in macOS Big Sur 11.6.1 or macOS Monterey.

sunknudsen
  • 6,356
  • 3
  • 39
  • 76