macos - 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.
解决方案
[yourNotificationCategory setValue:@"Your text" forKey:@"_actionsMenuTitle"];
这是用于更改操作按钮上的文本
推荐阅读
- reactjs - Enzyme-Jest React,我无法弄清楚为什么我的道具在模拟这个“变化”事件后没有更新。谢谢
- python - 在 python 中遇到 SSL 证书错误
- c - Does my program need to be able to handle SIGINT?
- debugging - 如何从 ghidra 的相应指令中破解 x64dbg?
- html - 如何摆脱菜单栏和侧面导航之间的空间?
- git - Bitbucket 管道 - 使用 SSH 密钥拉/推代码而没有权限被拒绝
- python-3.x - loop.run_in_executor 函数是否需要 asyncio.lock() 或 threading.Lock()?
- sql - Discord.py 和 SQL:并非所有成员都被添加到数据库中
- html - 如何使用 HTML、CSS、JS 为我的网站制作登录/注册代码
- wordpress - 显示未知 css 文件的 Pagespeed 见解