Reset UNUserNotification on macOS

Hi,

Since my app migrated from NSUserNotifications to UNUserNotification API I get 'ghost' notifications that will trigger regardless they were remove from NSUserNotificationCentre.

See sample code:

- (void)applicationDidFinishLaunching:(NSNotification*)notification {
	// Do all launch setup

	// Get notification center and install or update notification categories
	UNUserNotificationCenter*	center = [UNUserNotificationCenter currentNotificationCenter];
	[center getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory*>* categories) {
		if (categories.count == 0 || needNotificationUpdate) {
			UNNotificationCategory* reminderCategory = [UNNotificationCategory categoryWithIdentifier:kReminderIdentifier actions:@[[UNNotificationAction actionWithIdentifier:kActionIdentifier title:@"Action" options:UNNotificationActionOptionForeground]] intentIdentifiers:@[] options:UNNotificationCategoryOptionHiddenPreviewsShowTitle];
			[center setNotificationCategories:[NSSet setWithObject:reminderCategory]];
		}
	}];

	// Install daily reminder (hourly for the scope of testing)
	NSUserNotification*    userNotification = notification.userInfo[NSApplicationLaunchUserNotificationKey];

	if (userNotification == NULL && !self.foregroundNotification) {
		UNNotificationTrigger*  trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3600.0 repeats:YES];
		UNNotificationRequest*  request = [UNNotificationRequest requestWithIdentifier:kReminderIdentifier content:content trigger:trigger];
		[center addNotificationRequest:request withCompletionHandler:^(NSError* error){}];
	}
}

- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    self.foregroundNotification = YES;
    completionHandler(UNNotificationPresentationOptionAlert);
}

- (void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void (^)(void))completionHandler {
    
    if ([response.notification.request.identifier isEqualToString:kReminderIdentifier]) {
    	// Do notification action
    }
    
	UNUserNotificationCenter*	center = [UNUserNotificationCenter currentNotificationCenter];
	[center removePendingNotificationRequestsWithIdentifiers:@[kReminderIdentifier]];
	[center removeDeliveredNotificationsWithIdentifiers:@[kReminderIdentifier]];

    completionHandler();
    
    // At this point the notifications are removed from notification center
    // However they will come back one hour later. Those 'ghost' notification no longer launch the app or trigger actions.
}

I get those ghosts since weeks now and cannot find any way to get rid of them.

Is there a way to do a deep reset for all notifications of an app ?

Any advice is welcome.

Regards,

Jérôme Tarantino

Reset UNUserNotification on macOS
 
 
Q