Handle exception raised by Cocoa runtime when trying to get user notification center from a non-bundled executable

This commit is contained in:
Kovid Goyal 2024-03-13 11:43:38 +05:30
parent 5a9cf82564
commit a1b40cc8f5
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C

View file

@ -441,7 +441,13 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
static void
schedule_notification(const char *identifier, const char *title, const char *body, const char *subtitle) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNUserNotificationCenter *center = nil;
@try {
center = [UNUserNotificationCenter currentNotificationCenter];
} @catch (NSException *e) {
log_error("Failed to get current UNUserNotificationCenter object with error: %s (%s)",
[[e name] UTF8String], [[e reason] UTF8String]);
}
if (!center) return;
// Configure the notification's payload.
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
@ -503,7 +509,13 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
char *identifier = NULL, *title = NULL, *body = NULL, *subtitle = NULL;
if (!PyArg_ParseTuple(args, "zsz|z", &identifier, &title, &body, &subtitle)) return NULL;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNUserNotificationCenter *center = nil;
@try {
center = [UNUserNotificationCenter currentNotificationCenter];
} @catch (NSException *e) {
log_error("Failed to get current UNUserNotificationCenter object with error: %s (%s)",
[[e name] UTF8String], [[e reason] UTF8String]);
}
if (!center) Py_RETURN_NONE;
if (!center.delegate) center.delegate = [[NotificationDelegate alloc] init];
queue_notification(identifier, title, body, subtitle);