我需要将按钮添加到libnotify通知的底部,这些按钮在单击时运行函数。我可以让按钮出现,但它们在单击时不会运行函数。它根本没有给出错误消息。
使用./notifications "Title" "Body" "pathtoicon"调用该程序
代码:
#include <libnotify/notify.h>
#include <iostream>
void callback_mute(NotifyNotification* n, char* action, gpointer user_data) {
std::cout << "Muting Program" << std::endl;
system("pkexec kernel-notify -am");
}
int main(int argc, char * argv[] ) {
GError *error = NULL;
notify_init("Basics");
NotifyNotification* n = notify_notification_new (argv[1],
argv[2],
argv[3]);
notify_notification_add_action (n,
"action_click",
"Mute",
NOTIFY_ACTION_CALLBACK(callback_mute),
NULL,
NULL);
notify_notification_set_timeout(n, 10000);
if (!notify_notification_show(n, 0)) {
std::cerr << "Notification failed" << std::endl;
return 1;
}
return 0;
}如有任何帮助,将不胜感激,谢谢!
发布于 2019-09-18 08:04:20
您必须使用GMainLoop,一个“主事件循环”才能使回调函数工作。libnotify使用这个循环来处理它的操作,如果没有它,它就不会调用您期望的回调函数,因为没有任何东西可以处理它。
基本上,在你的main函数中,只需要在开头添加一个GMainLoop *loop,然后在之后添加loop = g_main_loop_new(nullptr, FALSE);来初始化它,然后在最后添加g_main_loop_run(loop);。你的程序应该像以前一样运行,但是回调函数现在可以工作了。所以基本上:
int main(int argc, char **argv)
{
GMainLoop *loop;
loop = g_main_loop_new(nullptr, FALSE);
// ... do your stuff
g_main_loop_run(loop);
return 0;
}有关它的更多信息,请访问:The Main Event Loop: GLib Reference Manual
您不需要包含glib.h,因为libnotify无论如何都会包含它。
https://stackoverflow.com/questions/57732550
复制相似问题