Curiously, with Monterey and subsequent macOS versions up to and including Sonoma, the Lazarus GUI program worked on some machines but not others for no reason I can determine (I have a stable of Mac minis :)
Anyway, while the Lazarus GUI version has this unreliability issue from Monterey onwards, my pure Pascal program below does not. It works on all machines and all macOS versions.
program badger;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
uses
CocoaAll;
type
{ TMyDelegate }
TMyDelegate = objcclass(NSObject)
public
procedure BadgeButtonClick(sender : id); message 'BadgeButtonClick:';
procedure UnBadgeButtonClick(sender : id); message 'UnBadgeButtonClick:';
end;
var
appName : NSString;
window : NSWindow;
myBadgeButton : NSButton;
myUnBadgeButton : NSButton;
myDelegate : TMyDelegate;
procedure TMyDelegate.BadgeButtonClick(sender : id);
begin
NSApp.dockTile.setBadgeLabel(NSStr('12'));
end;
procedure TMyDelegate.UnBadgeButtonClick(sender : id);
begin
NSApp.dockTile.setBadgeLabel(Nil);
end;
begin
// app and window creation
NSApp := NSApplication.sharedApplication;
NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
appName := NSStr('Badger');
window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask,
NSBackingStoreBuffered, False);
myDelegate := TMyDelegate.alloc.init;
// badge button
myBadgeButton := NSButton.alloc.initWithFrame(NSMakeRect(50, 100, 100, 50));
window.contentView.addSubview(myBadgeButton);
myBadgeButton.setTitle(NSSTR('Badge'));
myBadgeButton.setButtonType(NSMomentaryLightButton);
myBadgeButton.setBezelStyle(NSRoundedBezelStyle);
// unbadge button
myUnBadgeButton := NSButton.alloc.initWithFrame(NSMakeRect(50, 60, 100, 50));
window.contentView.addSubview(myUnBadgeButton);
myUnBadgeButton.setTitle(NSSTR('UnBadge'));
myUnBadgeButton.setButtonType(NSMomentaryLightButton);
myUnBadgeButton.setBezelStyle(NSRoundedBezelStyle);
// badge button event handler
myBadgeButton.setTarget(myDelegate);
myBadgeButton.setAction(ObjCSelector(myDelegate.BadgeButtonClick));
// unbadge button event handler
myUnBadgeButton.setTarget(myDelegate);
myUnBadgeButton.setAction(ObjCSelector(myDelegate.UnBadgeButtonClick));
// Window showing and app running
window.center;
window.setTitle(appName);
window.makeKeyAndOrderFront(Nil);
NSApp.activateIgnoringOtherApps(True);
NSApp.run;
end.
As to the UNUserNotificationCenter, this is currently not available in Lazarus (due to somewhat outdated Pascalised Cocoa headers), but thanks for responding!