Sending libnotify Notification through C

Published: by

  • Categories:

Libnotify is the nice way to notify users of the important events occurred. Following is the code to send libnotify notifications through C..

#include 
#include 
#include 
int main(int argc, char** argv)
{
	if(argc == 3)
	{
		NotifyNotification *n;
		notify_init("Test");
		n = notify_notification_new (argv[1],argv[2], NULL, NULL);
		notify_notification_set_timeout(n, 3000); //3 seconds
		if (!notify_notification_show (n, NULL)) {
			g_error("Failed to send notification.\n");
			return 1;
		}
		g_object_unref(G_OBJECT(n));
	}else{
		g_print("Too few arguments (%d), 2 needed.\n", argc-1);
	}
	return 0;
}

NOTE: As Eugene MechanisM pointed out, recent version has changed the API. See his comment for details.

While compiling the program, you need to link libraries… So, to compile and run….

shadyabhi@shadyabhi-desktop:~/c$ vim libnotify.c
shadyabhi@shadyabhi-desktop:~/c$ gcc -Wall -o test libnotify.c `pkg-config --libs --cflags glib-2.0 gtk+-2.0` -lnotify
shadyabhi@shadyabhi-desktop:~/c$ ./test "Heading" "Body of the notification"
shadyabhi@shadyabhi-desktop:~/c$