首页 > 解决方案 > how to change the notification close/action button text on macos catalina

问题描述

I'd like to modify the text of 'close' and 'action' buttons for my notifications in Notification Center on MacOS Catalina.

I know it was possible with NSUserNotification otherButtonTitle and actionButtonTitle but this is now deprecated. I can't find how to do the equivalent code with the new UNUserNotificationCenter API.

I'm able to add some UNNotificationAction to UNNotificationCategory but it does not change the default close/action buttons.

Anybody knows how to do that on MacOS?

So more precisely, with NSUserNotification API, this is the code I am using:

void NSOsxUserNotification::postNotification()
{
    auto notif = [[[NSUserNotification alloc] init] autorelease];
    notif.otherButtonTitle = [NSString stringWithCString:m_dict->getShowText().c_str() encoding:NSUTF8StringEncoding];
    notif.actionButtonTitle = [NSString stringWithCString:m_dict->getNotNowText().c_str() encoding:NSUTF8StringEncoding];
    notif.title = [NSString stringWithCString:m_title.c_str() encoding:NSUTF8StringEncoding];
    notif.informativeText = [NSString stringWithCString:m_message.c_str() encoding:NSUTF8StringEncoding];
    notif.hasActionButton = true;

    NSMutableArray* additionalActions = [NSMutableArray array];
    [additionalActions addObject: [NSUserNotificationAction actionWithIdentifier:@"Later" title:[NSString stringWithCString:m_dict->getLaterText().c_str() encoding:NSUTF8StringEncoding]]];
    [additionalActions addObject: [NSUserNotificationAction actionWithIdentifier:@"Never" title:[NSString stringWithCString:m_dict->getNeverText().c_str() encoding:NSUTF8StringEncoding]]];

    notif.additionalActions = additionalActions;

    [notif setValue:@YES forKey:@"_alwaysShowAlternateActionMenu"];

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notif];
}

And it gives the result I want: Notification with NSUserNotification API

Now I have to use the new API UNUserNotificationCenter:

UNOsxUserNotification::UNOsxUserNotification(dictionary::IDictionaryPtr dict) : OsxUserNotification(dict)
{
    auto handler = [[UNNotificationCentreDelegate alloc] initialise: this];
    m_notificationCentre = handler;
    [[UNUserNotificationCenter currentNotificationCenter] setDelegate:handler];

    auto showAction = [UNNotificationAction actionWithIdentifier:@"SHOW_ACTION" title:[NSString stringWithCString:m_dict->getShowText().c_str() encoding:NSUTF8StringEncoding] options:UNNotificationActionOptionForeground];
    auto notNowAction = [UNNotificationAction actionWithIdentifier:@"NOT_NOW_ACTION" title:[NSString stringWithCString:m_dict->getNotNowText().c_str() encoding:NSUTF8StringEncoding] options:UNNotificationActionOptionNone];
    auto laterAction = [UNNotificationAction actionWithIdentifier:@"LATER_ACTION" title:[NSString stringWithCString:m_dict->getLaterText().c_str() encoding:NSUTF8StringEncoding] options:UNNotificationActionOptionNone];
    auto neverAction = [UNNotificationAction actionWithIdentifier:@"NEVER_ACTION" title:[NSString stringWithCString:m_dict->getNeverText().c_str() encoding:NSUTF8StringEncoding] options:UNNotificationActionOptionNone];

    auto publicationCategory = [UNNotificationCategory categoryWithIdentifier:@"NEW_PUBLICATION_CATEGORY" actions:@[showAction, notNowAction, laterAction, neverAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

    auto center = [UNUserNotificationCenter currentNotificationCenter];
    [center setNotificationCategories:[NSSet setWithObjects:publicationCategory, nil]];
}

void UNOsxUserNotification::publicNotification()
{
    auto content = [[[UNMutableNotificationContent alloc] init] autorelease];
    content.title = [NSString stringWithCString:m_title.c_str() encoding:NSUTF8StringEncoding];
    content.body = [NSString stringWithCString:m_message.c_str() encoding:NSUTF8StringEncoding];
    content.categoryIdentifier = @"NEW_PUBLICATION_CATEGORY";

    auto request = [UNNotificationRequest requestWithIdentifier:@"NEW_PUBLICATION_REQUEST_ID" content:content trigger:nil];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (error)
        {
            // Log error
            return;
        }

        onDeliverNotify();
    }];
}

Unfortunately, it doesn't give the result I want: Notification with UNUserNotification API

I do not want the close and actions text, I want to replace them.

标签: macosmacos-catalinanotificationcenter

解决方案


[yourNotificationCategory setValue:@"Your text" forKey:@"_actionsMenuTitle"];

这是用于更改操作按钮上的文本


推荐阅读