Notification mit UNUserNotificationCenterMBS

Hallo!

Mein System: macOS 26.6.2

XOJO Version 2026 Release 2.1

MBS Xojo Plugins 26.1

Ich habe zwei Fragen zu UNUserNotificationCenterMBS:

  • Ich habe im Ereignis "willPresentNotifikation mit der Zuweisung options = NotificationPresentationOptionAlert implementiert. Das Ereignis wird aber jedesmal erst ab dem zweiten NotificationCenter.addNotificationRequest(myRequest,nil) durchlaufen. Wie kann man es einstellen, daß es schon beim ersten NotificationRequest durchlaufen wird.
  • Wenn meine App geschlossen wird und eine Datenbankänderung festgestellt worden ist, dann wird im DesktopWindow.Closing noch ein BackUp angestoßen. Das dauert einen Moment. Daher habe ich in DesktopWindow.Closing zwei Aufrufe des NotificationRequest. Der erste Aufruf ist vor dem Start des BackUp, der zweite nach dem Aufruf des BackUp. Der erste Aufruf landet jedes Mal verdeckt im Mitteilungszentrum, der zweite Aufruf wird wie gewünscht vorübergehend auf dem Schreibtisch angezeigt. Gibt es eine Möglichkeit aus dem Ereignis Closing heraus auch den ersten NotificationRequest vorübergehend auf dem Schreibtisch erscheinen zu lassen?
Sub willPresentNotification(notification as UNNotificationMBS, byref options as Integer) Handles willPresentNotification
  options = NotificationPresentationOptionAlert
End Sub

#if TargetMacOS then 
  
  if UNUserNotificationCenterMBS.Available then
    
    if NotificationCenter = nil then
      NotificationCenter = new KlasseNotificationCenter
      if NotificationCenter <> nil then
        NotificationCenter.requestAuthorization(7)
      else
        MessageBox(cc_NotificationCenterNotFound) 'Mitteilungszentrale nicht gefunden
        Return
      end if
    end if
    
    Var myNotification as new UNMutableNotificationContentMBS()
    myNotification.title    = Title
    myNotification.subtitle = Subtitle
    myNotification.body     = Body
    
    Var myTrigger as new UNTimeIntervalNotificationTriggerMBS(NotificationInterval,False)
    
    Var myRequest as new UNNotificationRequestMBS(app.ExecutableFile.Name, myNotification,myTrigger)
    ' On Mojave and Catalina we could pass nil for the trigger, when sending the "immediate" option.  But we found we couldn't on Big Sur.
    ' Also, we cannot zero for the trigger, because it will throw an NSException.  So we settled for .1
    NotificationCenter.addNotificationRequest(myRequest,nil)
    
  else
    MessageBox(cc_NotificationCenterNotAvailable) 'User-Notification is not available. Bitte Entwickler benachrichtigen
    return
  end if
  
#ElseIf TargetWindows then

Dein Backup läuft in einem Thread im Hintergrund?

Weil wenn der Mainthread beschäftigt ist, kommen eventuell asynchrone Aufrufe später an.
Die Notification vom Anfang vom Backup bleibt also eventuell liegen.

Nein, der Backup läuft im Mainthread. Da die App gerade beendet werden soll habe ich keinen Grund für einen Extra-Thread gesehen. Ja, da wird vermutlich der asynchrone Aufruf hängen bleiben. → Das ist dann zwar keine Lösung aber die Antwort auf meine zweite Frage. Vielen Dank!!

Aber warum wird willPresentNotification nicht schon beim ersten Aufruf des Notification Center (UNUserNotificationCenterMBS) aufgerufen?

Das entscheidet Apple im Framework.
Vermutlich wird ein paar Sekunden gesammelt, ob noch was kommt, bevor es angezeigt wird.