gem-graph-client/doc/GTK-docs/gnome-dev-documentation/developer.gnome.org/documentation/tutorials/notifications.html

595 lines
63 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html class="no-js">
<head><meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta name="color-scheme" content="light dark"><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<link rel="index" title="Index" href="../genindex.html" /><link rel="search" title="Search" href="../search.html" /><link rel="next" title="Themed Icons" href="themed-icons.html" /><link rel="prev" title="Saving and Loading Window State" href="save-state.html" />
<meta name="generator" content="sphinx-4.3.0, furo 2022.06.21"/>
<title>Using Notifications - GNOME Developer Documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/styles/furo.css?digest=40978830699223671f4072448e654b5958f38b89" />
<link rel="stylesheet" type="text/css" href="../_static/tabs.css" />
<link rel="stylesheet" type="text/css" href="../_static/styles/furo-extensions.css?digest=30d1aed668e5c3a91c3e3bf6a60b675221979f0e" />
<link rel="stylesheet" type="text/css" href="../_static/gnome.css" />
<style>
body {
--color-code-background: #f8f8f8;
--color-code-foreground: black;
--color-brand-primary: #4a86cf;
--color-brand-content: #4a86cf;
}
@media not print {
body[data-theme="dark"] {
--color-code-background: #202020;
--color-code-foreground: #d0d0d0;
}
@media (prefers-color-scheme: dark) {
body:not([data-theme="light"]) {
--color-code-background: #202020;
--color-code-foreground: #d0d0d0;
}
}
}
</style></head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<symbol id="svg-toc" viewBox="0 0 24 24">
<title>Contents</title>
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024">
<path d="M408 442h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8zm-8 204c0 4.4 3.6 8 8 8h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56zm504-486H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 632H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM115.4 518.9L271.7 642c5.8 4.6 14.4.5 14.4-6.9V388.9c0-7.4-8.5-11.5-14.4-6.9L115.4 505.1a8.74 8.74 0 0 0 0 13.8z"/>
</svg>
</symbol>
<symbol id="svg-menu" viewBox="0 0 24 24">
<title>Menu</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-menu">
<line x1="3" y1="12" x2="21" y2="12"></line>
<line x1="3" y1="6" x2="21" y2="6"></line>
<line x1="3" y1="18" x2="21" y2="18"></line>
</svg>
</symbol>
<symbol id="svg-arrow-right" viewBox="0 0 24 24">
<title>Expand</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-chevron-right">
<polyline points="9 18 15 12 9 6"></polyline>
</svg>
</symbol>
<symbol id="svg-sun" viewBox="0 0 24 24">
<title>Light mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather-sun">
<circle cx="12" cy="12" r="5"></circle>
<line x1="12" y1="1" x2="12" y2="3"></line>
<line x1="12" y1="21" x2="12" y2="23"></line>
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
<line x1="1" y1="12" x2="3" y2="12"></line>
<line x1="21" y1="12" x2="23" y2="12"></line>
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
</svg>
</symbol>
<symbol id="svg-moon" viewBox="0 0 24 24">
<title>Dark mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-moon">
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
<path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" />
</svg>
</symbol>
<symbol id="svg-sun-half" viewBox="0 0 24 24">
<title>Auto light/dark mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-shadow">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<circle cx="12" cy="12" r="9" />
<path d="M13 12h5" />
<path d="M13 15h4" />
<path d="M13 18h1" />
<path d="M13 9h4" />
<path d="M13 6h1" />
</svg>
</symbol>
</svg>
<input type="checkbox" class="sidebar-toggle" name="__navigation" id="__navigation">
<input type="checkbox" class="sidebar-toggle" name="__toc" id="__toc">
<label class="overlay sidebar-overlay" for="__navigation"></label>
<label class="overlay toc-overlay" for="__toc"></label>
<div class="page">
<header class="mobile-header">
<div class="header-left">
<label class="nav-overlay-icon" for="__navigation">
<i class="icon"><svg><use href="#svg-menu"></use></svg></i>
</label>
</div>
<div class="header-center">
<a href="../index.html"><div class="brand">GNOME Developer Documentation</div></a>
</div>
<div class="header-right">
<label class="toc-overlay-icon toc-header-icon" for="__toc">
<i class="icon"><svg><use href="#svg-toc"></use></svg></i>
</label>
</div>
</header>
<aside class="sidebar-drawer">
<div class="sidebar-container">
<div class="sidebar-sticky"><a class="sidebar-brand" href="../index.html">
<span class="sidebar-brand-text">GNOME Developer Documentation</span>
</a><form class="sidebar-search-container" method="get" action="../search.html" role="search">
<input class="sidebar-search" placeholder=Search name="q" aria-label="Search">
<input type="hidden" name="check_keywords" value="yes">
<input type="hidden" name="area" value="default">
</form>
<div id="searchbox"></div><div class="sidebar-scroll"><div class="sidebar-tree">
<p class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current">
<li class="toctree-l1 has-children"><a class="reference internal" href="../introduction.html">Platform Introduction</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" role="switch" type="checkbox"/><label for="toctree-checkbox-1"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l2 has-children"><a class="reference internal" href="../introduction/components.html">Platform Components</a><input class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" role="switch" type="checkbox"/><label for="toctree-checkbox-2"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../introduction/overview/libraries.html">Libraries</a></li>
<li class="toctree-l3"><a class="reference internal" href="../introduction/overview/services.html">Services</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../introduction/languages.html">Programming Languages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../introduction/builder.html">GNOME Builder</a></li>
<li class="toctree-l2"><a class="reference internal" href="../introduction/flatpak.html">Flatpak</a></li>
</ul>
</li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../guidelines.html">Guidelines</a><input class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" role="switch" type="checkbox"/><label for="toctree-checkbox-3"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l2 has-children"><a class="reference internal" href="../guidelines/programming.html">Programming Guidelines</a><input class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" role="switch" type="checkbox"/><label for="toctree-checkbox-4"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/programming/coding-style.html">C Coding Style</a></li>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/programming/memory-management.html">Managing Memory</a></li>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/programming/writing-good-code.html">The Importance of Writing Good Code</a></li>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/programming/optimizing.html">Optimizing GNOME Applications</a></li>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/programming/namespacing.html">Namespacing</a></li>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/programming/introspection.html">Introspection</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../guidelines/accessibility.html">Accessibility</a><input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" role="switch" type="checkbox"/><label for="toctree-checkbox-5"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/accessibility/coding-guidelines.html">Coding Guidelines for Supporting Accessibility</a></li>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/accessibility/custom-widgets.html">Making Custom Components Accessible</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../guidelines/localization.html">Localization</a><input class="toctree-checkbox" id="toctree-checkbox-6" name="toctree-checkbox-6" role="switch" type="checkbox"/><label for="toctree-checkbox-6"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/localization/practices.html">Best Practices for Localization</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../guidelines/maintainer.html">Maintainer Guidelines</a><input class="toctree-checkbox" id="toctree-checkbox-7" name="toctree-checkbox-7" role="switch" type="checkbox"/><label for="toctree-checkbox-7"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/maintainer/api-stability.html">API Stability</a></li>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/maintainer/parallel-installability.html">Parallel Installability</a></li>
<li class="toctree-l3"><a class="reference internal" href="../guidelines/maintainer/integrating.html">Integrating with GNOME</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../guidelines/devel-docs.html">Developer Documentation Style Guidelines</a></li>
</ul>
</li>
<li class="toctree-l1 current has-children"><a class="reference internal" href="../tutorials.html">Tutorials</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-8" name="toctree-checkbox-8" role="switch" type="checkbox"/><label for="toctree-checkbox-8"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul class="current">
<li class="toctree-l2 has-children"><a class="reference internal" href="beginners.html">Beginners Tutorials</a><input class="toctree-checkbox" id="toctree-checkbox-9" name="toctree-checkbox-9" role="switch" type="checkbox"/><label for="toctree-checkbox-9"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3 has-children"><a class="reference internal" href="beginners/getting_started.html">Getting Started</a><input class="toctree-checkbox" id="toctree-checkbox-10" name="toctree-checkbox-10" role="switch" type="checkbox"/><label for="toctree-checkbox-10"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l4"><a class="reference internal" href="beginners/getting_started/content_view.html">Adding A Content View</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/getting_started/opening_files.html">Loading Content From A File</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/getting_started/cursor_position.html">Showing The Cursor Position</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/getting_started/saving_files.html">Saving The Content To A File</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/getting_started/saving_state.html">Saving The Application State</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/getting_started/adding_toasts.html">Notifying The User With Toasts</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/getting_started/dark_mode.html">Forcing The Dark Color Scheme</a></li>
</ul>
</li>
<li class="toctree-l3 has-children"><a class="reference internal" href="beginners/components.html">UI components</a><input class="toctree-checkbox" id="toctree-checkbox-11" name="toctree-checkbox-11" role="switch" type="checkbox"/><label for="toctree-checkbox-11"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/window.html">Windows</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/image.html">Images</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/label.html">Labels</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/button.html">Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/box.html">Boxes</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/stack.html">Stacks</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/leaflet.html">Leaflets</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/toggle.html">Toggles</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/check_box.html">Check Boxes</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/radio_button.html">Radio Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/spin_button.html">Spin Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/link_button.html">Link Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/switch.html">Switches</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/menu_button.html">Menu Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/entry.html">Entries</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/password_entry.html">Password Entries</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/message_dialog.html">Messages</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/file_dialog.html">File Dialogs</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/spinner.html">Spinners</a></li>
<li class="toctree-l4"><a class="reference internal" href="beginners/components/level_bar.html">Level Bars</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="application-id.html">Application ID</a></li>
<li class="toctree-l2"><a class="reference internal" href="application.html">Using GtkApplication</a></li>
<li class="toctree-l2"><a class="reference internal" href="save-state.html">Saving and Loading Window State</a></li>
<li class="toctree-l2 current current-page"><a class="current reference internal" href="#">Using Notifications</a></li>
<li class="toctree-l2"><a class="reference internal" href="themed-icons.html">Themed Icons</a></li>
<li class="toctree-l2"><a class="reference internal" href="deprecations.html">Dealing With Deprecations</a></li>
<li class="toctree-l2"><a class="reference internal" href="actions.html">Actions</a></li>
<li class="toctree-l2"><a class="reference internal" href="menus.html">Menus</a></li>
<li class="toctree-l2"><a class="reference internal" href="search-provider.html">Writing a Search Provider</a></li>
<li class="toctree-l2"><a class="reference internal" href="pre-and-post-conditions.html">Pre- and Post-Conditions</a></li>
<li class="toctree-l2"><a class="reference internal" href="main-contexts.html">Main Contexts</a></li>
<li class="toctree-l2"><a class="reference internal" href="lists.html">Using GLib Lists</a></li>
<li class="toctree-l2"><a class="reference internal" href="threading.html">Threading</a></li>
<li class="toctree-l2"><a class="reference internal" href="asynchronous-programming.html">Asynchronous Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="drag-and-drop.html">Drag and Drop</a></li>
<li class="toctree-l2"><a class="reference internal" href="widget-templates.html">Widget Templates</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
</aside>
<main class="main">
<div class="content">
<article role="main">
<label class="toc-overlay-icon toc-content-icon" for="__toc">
<i class="icon"><svg><use href="#svg-toc"></use></svg></i>
</label>
<section id="using-notifications">
<h1>Using Notifications<a class="headerlink" href="#using-notifications" title="Permalink to this headline">#</a></h1>
<p>GNOME applications should use notifications to inform the user that something
has happened that requires their attention.</p>
<p>Notifications should not be intrusive, or distracting.</p>
<p>For more information on when to use notifications, you should follow the <a class="reference external" href="https://developer.gnome.org/hig/patterns/feedback/notifications.html">Human
Interface Guidelines</a>.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>Remember that users can disable notifications for specific applications, or
globally. You should not rely exclusively on notifications.</p>
</div>
<section id="prerequisites">
<h2>Prerequisites<a class="headerlink" href="#prerequisites" title="Permalink to this headline">#</a></h2>
<p>In order to use notifications in GNOME you will need to:</p>
<ul class="simple">
<li><p>use <code class="docutils literal notranslate"><span class="pre">GApplication</span></code> or <code class="docutils literal notranslate"><span class="pre">GtkApplication</span></code></p></li>
<li><p>provide a valid desktop file with the same name as your <a class="reference external" href="application-id">application id</a></p></li>
<li><p>ensure that your application can be activated via D-Bus</p></li>
</ul>
<p>The desktop file and D-Bus activation provide notification persistence, allowing
the desktop to associate the notification to the application even if the
application is not running.</p>
</section>
<section id="anatomy-of-a-notification">
<h2>Anatomy of a notification<a class="headerlink" href="#anatomy-of-a-notification" title="Permalink to this headline">#</a></h2>
<p>A typical notification has a number of ingredients:</p>
<ul class="simple">
<li><p>a one-line <strong>title</strong></p></li>
<li><p>a longer, descriptive <strong>message body</strong> (optional)</p></li>
<li><p>an <strong>icon</strong> (optional)</p></li>
<li><p><strong>actions</strong>, each with a label for use in a button (optional)</p></li>
<li><p>additionally, notifications may be marked as <strong>urgent</strong></p></li>
</ul>
</section>
<section id="creating-a-notification">
<h2>Creating a notification<a class="headerlink" href="#creating-a-notification" title="Permalink to this headline">#</a></h2>
<p>To send a notification, first create a <code class="docutils literal notranslate"><span class="pre">GNotification</span></code> object, and add the
data for your notification to it:</p>
<div class="sphinx-tabs docutils container">
<div aria-label="Tabbed content" class="closeable" role="tablist"><button aria-controls="panel-0-Qw==" aria-selected="true" class="sphinx-tabs-tab code-tab group-tab" id="tab-0-Qw==" name="Qw==" role="tab" tabindex="0">C</button><button aria-controls="panel-0-UHl0aG9u" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-0-UHl0aG9u" name="UHl0aG9u" role="tab" tabindex="-1">Python</button><button aria-controls="panel-0-VmFsYQ==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-0-VmFsYQ==" name="VmFsYQ==" role="tab" tabindex="-1">Vala</button><button aria-controls="panel-0-SmF2YVNjcmlwdA==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-0-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tab" tabindex="-1">JavaScript</button></div><div aria-labelledby="tab-0-Qw==" class="sphinx-tabs-panel code-tab group-tab" id="panel-0-Qw==" name="Qw==" role="tabpanel" tabindex="0"><div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">g_autoptr</span><span class="p">(</span><span class="n">GNotification</span><span class="p">)</span><span class="w"> </span><span class="n">notification</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">g_notification_new</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;Lunch is ready&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">g_notification_set_body</span><span class="w"> </span><span class="p">(</span><span class="n">notification</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Today we have pancakes and salad, and fruit and cake for dessert&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">g_autoptr</span><span class="p">(</span><span class="n">GFile</span><span class="p">)</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">g_file_new_for_path</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;fruitbowl.png&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">g_autoptr</span><span class="p">(</span><span class="n">GFileIcon</span><span class="p">)</span><span class="w"> </span><span class="n">icon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">g_file_icon_new</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">);</span><span class="w"></span>
<span class="c1">// The notification instance will acquire a reference on the icon</span>
<span class="n">g_notification_set_icon</span><span class="w"> </span><span class="p">(</span><span class="n">notification</span><span class="p">,</span><span class="w"> </span><span class="n">G_ICON</span><span class="w"> </span><span class="p">(</span><span class="n">icon</span><span class="p">));</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-0-UHl0aG9u" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-0-UHl0aG9u" name="UHl0aG9u" role="tabpanel" tabindex="0"><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">notification</span> <span class="o">=</span> <span class="n">Gio</span><span class="o">.</span><span class="n">Notification</span><span class="p">()</span>
<span class="n">notification</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s2">&quot;Lunch is ready&quot;</span><span class="p">)</span>
<span class="n">notification</span><span class="o">.</span><span class="n">set_body</span><span class="p">(</span><span class="s2">&quot;Today we have pancakes and salad, and fruit and cake for dessert&quot;</span><span class="p">)</span>
<span class="n">file</span> <span class="o">=</span> <span class="n">Gio</span><span class="o">.</span><span class="n">File</span><span class="o">.</span><span class="n">new_for_path</span><span class="p">(</span><span class="s2">&quot;fruitbowl.png&quot;</span><span class="p">)</span>
<span class="n">icon</span> <span class="o">=</span> <span class="n">Gio</span><span class="o">.</span><span class="n">FileIcon</span><span class="p">(</span><span class="n">file</span><span class="o">=</span><span class="n">file</span><span class="p">)</span>
<span class="n">notification</span><span class="o">.</span><span class="n">set_icon</span><span class="p">(</span><span class="n">icon</span><span class="p">)</span>
</pre></div>
</div>
</div><div aria-labelledby="tab-0-VmFsYQ==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-0-VmFsYQ==" name="VmFsYQ==" role="tabpanel" tabindex="0"><div class="highlight-vala notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">notification</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Notification</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;Lunch is ready&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">notification</span><span class="p">.</span><span class="n">set_body</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;Today we have pancakes and salad, and fruit and cake for dessert&quot;</span><span class="p">);</span><span class="w"></span>
<span class="kd">var</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">File</span><span class="p">.</span><span class="n">for_path</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;fruitbowl.png&quot;</span><span class="p">);</span><span class="w"></span>
<span class="kd">var</span><span class="w"> </span><span class="n">icon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FileIcon</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">);</span><span class="w"></span>
<span class="c1">// The notification instance will acquire a reference on the icon</span>
<span class="n">notification</span><span class="p">.</span><span class="n">set_icon</span><span class="w"> </span><span class="p">(</span><span class="n">icon</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-0-SmF2YVNjcmlwdA==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-0-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tabpanel" tabindex="0"><div class="highlight-js notranslate"><div class="highlight"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">notification</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Gio</span><span class="p">.</span><span class="nx">Notification</span><span class="p">();</span><span class="w"></span>
<span class="nx">notification</span><span class="p">.</span><span class="nx">set_title</span><span class="p">(</span><span class="s2">&quot;Lunch is ready&quot;</span><span class="p">);</span><span class="w"></span>
<span class="nx">notification</span><span class="p">.</span><span class="nx">set_body</span><span class="p">(</span><span class="w"></span>
<span class="w"> </span><span class="s2">&quot;Today we have pancakes and salad, and fruit and cake for dessert&quot;</span><span class="p">,</span><span class="w"></span>
<span class="p">);</span><span class="w"></span>
<span class="kd">const</span><span class="w"> </span><span class="nx">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Gio</span><span class="p">.</span><span class="nx">File</span><span class="p">.</span><span class="nx">new_for_path</span><span class="p">(</span><span class="s2">&quot;fruitbowl.png&quot;</span><span class="p">);</span><span class="w"></span>
<span class="kd">const</span><span class="w"> </span><span class="nx">icon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Gio</span><span class="p">.</span><span class="nx">FileIcon</span><span class="p">({</span><span class="nx">file</span><span class="p">});</span><span class="w"></span>
<span class="c1">// The notification instance will acquire a reference on the icon</span><span class="w"></span>
<span class="nx">notification</span><span class="p">.</span><span class="nx">set_icon</span><span class="p">(</span><span class="nx">icon</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div></div>
<p>Note that the title should be short; the body can be longer, say a paragraph.
The icon may be displayed at a small size (say, 24×24), so choose an icon that
is remains readable at small size.</p>
<p>To show your notification to the user, use the <code class="docutils literal notranslate"><span class="pre">GApplication</span></code> function for this
purpose:</p>
<div class="sphinx-tabs docutils container">
<div aria-label="Tabbed content" class="closeable" role="tablist"><button aria-controls="panel-1-Qw==" aria-selected="true" class="sphinx-tabs-tab code-tab group-tab" id="tab-1-Qw==" name="Qw==" role="tab" tabindex="0">C</button><button aria-controls="panel-1-UHl0aG9u" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-1-UHl0aG9u" name="UHl0aG9u" role="tab" tabindex="-1">Python</button><button aria-controls="panel-1-VmFsYQ==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-1-VmFsYQ==" name="VmFsYQ==" role="tab" tabindex="-1">Vala</button><button aria-controls="panel-1-SmF2YVNjcmlwdA==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-1-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tab" tabindex="-1">JavaScript</button></div><div aria-labelledby="tab-1-Qw==" class="sphinx-tabs-panel code-tab group-tab" id="panel-1-Qw==" name="Qw==" role="tabpanel" tabindex="0"><div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="c1">// The application instance will acquire a reference on the</span>
<span class="c1">// notification object</span>
<span class="n">g_application_send_notification</span><span class="w"> </span><span class="p">(</span><span class="n">application</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;lunch-is-ready&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">notification</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-1-UHl0aG9u" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-1-UHl0aG9u" name="UHl0aG9u" role="tabpanel" tabindex="0"><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># The Application instance will acquire a reference on the</span>
<span class="c1"># notification object</span>
<span class="n">application</span><span class="o">.</span><span class="n">send_notification</span><span class="p">(</span><span class="s2">&quot;lunch-is-ready&quot;</span><span class="p">,</span> <span class="n">notification</span><span class="p">)</span>
</pre></div>
</div>
</div><div aria-labelledby="tab-1-VmFsYQ==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-1-VmFsYQ==" name="VmFsYQ==" role="tabpanel" tabindex="0"><div class="highlight-vala notranslate"><div class="highlight"><pre><span></span><span class="c1">// The application instance will acquire a reference on the</span>
<span class="c1">// notification object</span>
<span class="n">application</span><span class="p">.</span><span class="n">send_notification</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;lunch-is-ready&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">notification</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-1-SmF2YVNjcmlwdA==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-1-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tabpanel" tabindex="0"><div class="highlight-js notranslate"><div class="highlight"><pre><span></span><span class="c1">// The application instance will acquire a reference on the</span><span class="w"></span>
<span class="c1">// notification object</span><span class="w"></span>
<span class="nx">application</span><span class="p">.</span><span class="nx">send_notification</span><span class="p">(</span><span class="s2">&quot;lunch-is-ready&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">notification</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div></div>
<p>You need to provide an id for your notification here. This can be used if you
want to make updates to an existing notification: simply send a notification
with the same id. Note that the GNotification object does not have to be kept
around after sending the notification; you can unref it right away. It is not a
live object that is associated with the visible notification.</p>
</section>
<section id="adding-actions">
<h2>Adding actions<a class="headerlink" href="#adding-actions" title="Permalink to this headline">#</a></h2>
<p>Often, you want the user to be able to react to the notification in some way,
other than just dismissing it. <code class="docutils literal notranslate"><span class="pre">GNotification</span></code> lets you do this by associating
actions with your notification. These will typically be presented as buttons in
the popup. One action has a special role, it is the default action that gets
activated when the user clicks on the notification, not on a particular button.</p>
<div class="sphinx-tabs docutils container">
<div aria-label="Tabbed content" class="closeable" role="tablist"><button aria-controls="panel-2-Qw==" aria-selected="true" class="sphinx-tabs-tab code-tab group-tab" id="tab-2-Qw==" name="Qw==" role="tab" tabindex="0">C</button><button aria-controls="panel-2-UHl0aG9u" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-2-UHl0aG9u" name="UHl0aG9u" role="tab" tabindex="-1">Python</button><button aria-controls="panel-2-VmFsYQ==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-2-VmFsYQ==" name="VmFsYQ==" role="tab" tabindex="-1">Vala</button><button aria-controls="panel-2-SmF2YVNjcmlwdA==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-2-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tab" tabindex="-1">JavaScript</button></div><div aria-labelledby="tab-2-Qw==" class="sphinx-tabs-panel code-tab group-tab" id="panel-2-Qw==" name="Qw==" role="tabpanel" tabindex="0"><div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">g_notification_set_default_action</span><span class="w"> </span><span class="p">(</span><span class="n">notification</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;app.go-to-lunch&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">g_notification_add_button</span><span class="w"> </span><span class="p">(</span><span class="n">notification</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;5 minutes&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;app.reply-5-minutes&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">g_notification_add_button</span><span class="w"> </span><span class="p">(</span><span class="n">notification</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Order takeout&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;app.order-takeout&quot;</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-2-UHl0aG9u" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-2-UHl0aG9u" name="UHl0aG9u" role="tabpanel" tabindex="0"><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">notification</span><span class="o">.</span><span class="n">set_default_action</span><span class="p">(</span><span class="s2">&quot;app.go-to-lunch&quot;</span><span class="p">)</span>
<span class="n">notification</span><span class="o">.</span><span class="n">add_button</span><span class="p">(</span><span class="s2">&quot;5 minutes&quot;</span><span class="p">,</span> <span class="s2">&quot;app.reply-5-minutes&quot;</span><span class="p">)</span>
<span class="n">notification</span><span class="o">.</span><span class="n">add_button</span><span class="p">(</span><span class="s2">&quot;Order takeout&quot;</span><span class="p">,</span> <span class="s2">&quot;app.order-takeout&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div><div aria-labelledby="tab-2-VmFsYQ==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-2-VmFsYQ==" name="VmFsYQ==" role="tabpanel" tabindex="0"><div class="highlight-vala notranslate"><div class="highlight"><pre><span></span><span class="n">notification</span><span class="p">.</span><span class="n">set_default_action</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;app.go-to-lunch&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">notification</span><span class="p">.</span><span class="n">add_button</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;5 minutes&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;app.reply-5-minutes&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">notification</span><span class="p">.</span><span class="n">add_button</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;Order takeout&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;app.order-takeout&quot;</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-2-SmF2YVNjcmlwdA==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-2-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tabpanel" tabindex="0"><div class="highlight-js notranslate"><div class="highlight"><pre><span></span><span class="nx">notification</span><span class="p">.</span><span class="nx">set_default_action</span><span class="p">(</span><span class="s2">&quot;app.go-to-lunch&quot;</span><span class="p">);</span><span class="w"></span>
<span class="nx">notification</span><span class="p">.</span><span class="nx">add_button</span><span class="p">(</span><span class="s2">&quot;5 minutes&quot;</span><span class="p">,</span><span class="w"> </span><span class="s2">&quot;app.reply-5-minutes&quot;</span><span class="p">);</span><span class="w"></span>
<span class="nx">notification</span><span class="p">.</span><span class="nx">add_button</span><span class="p">(</span><span class="s2">&quot;Order takeout&quot;</span><span class="p">,</span><span class="w"> </span><span class="s2">&quot;app.order-takeout&quot;</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div></div>
<p>The actions are referred to here with their app. prefixed name. This indicates
that the actions have to be added to your <code class="docutils literal notranslate"><span class="pre">GApplication</span></code>. You can not use any
other actions in <code class="docutils literal notranslate"><span class="pre">GNotifications</span></code> (window-specific actions with a win. prefix,
or key shorcuts using other prefixes will not work).</p>
</section>
<section id="actions-with-parameters">
<h2>Actions with parameters<a class="headerlink" href="#actions-with-parameters" title="Permalink to this headline">#</a></h2>
<p>A common pattern is to pass a target parameter to the action that contains
sufficient details about the notification to let your application react in a
meaningful way.</p>
<p>As an example, here is how a notification about a newly installed application
could provide a launch button:</p>
<div class="sphinx-tabs docutils container">
<div aria-label="Tabbed content" class="closeable" role="tablist"><button aria-controls="panel-3-Qw==" aria-selected="true" class="sphinx-tabs-tab code-tab group-tab" id="tab-3-Qw==" name="Qw==" role="tab" tabindex="0">C</button><button aria-controls="panel-3-UHl0aG9u" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-3-UHl0aG9u" name="UHl0aG9u" role="tab" tabindex="-1">Python</button><button aria-controls="panel-3-VmFsYQ==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-3-VmFsYQ==" name="VmFsYQ==" role="tab" tabindex="-1">Vala</button><button aria-controls="panel-3-SmF2YVNjcmlwdA==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-3-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tab" tabindex="-1">JavaScript</button></div><div aria-labelledby="tab-3-Qw==" class="sphinx-tabs-panel code-tab group-tab" id="panel-3-Qw==" name="Qw==" role="tabpanel" tabindex="0"><div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">g_autofree</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">g_strdup_printf</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;%s is now installed&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">appname</span><span class="p">);</span><span class="w"></span>
<span class="n">g_autoptr</span><span class="p">(</span><span class="n">GNotification</span><span class="p">)</span><span class="w"> </span><span class="n">notification</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">g_notification_new</span><span class="w"> </span><span class="p">(</span><span class="n">title</span><span class="p">);</span><span class="w"></span>
<span class="n">g_notification_add_button_with_target</span><span class="w"> </span><span class="p">(</span><span class="n">notification</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Launch&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;app.launch&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;s&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">appid</span><span class="p">);</span><span class="w"></span>
<span class="n">g_application_send_notification</span><span class="w"> </span><span class="p">(</span><span class="n">application</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;app-installed&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">notification</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-3-UHl0aG9u" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-3-UHl0aG9u" name="UHl0aG9u" role="tabpanel" tabindex="0"><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">notification</span> <span class="o">=</span> <span class="n">Gio</span><span class="o">.</span><span class="n">Notification</span><span class="p">()</span>
<span class="n">notification</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">appname</span><span class="si">}</span><span class="s2"> is now installed&quot;</span><span class="p">)</span>
<span class="n">notification</span><span class="o">.</span><span class="n">add_button_with_target</span><span class="p">(</span><span class="s2">&quot;Launch&quot;</span><span class="p">,</span> <span class="s2">&quot;app.launch&quot;</span><span class="p">,</span> <span class="s2">&quot;s&quot;</span><span class="p">,</span> <span class="n">appid</span><span class="p">)</span>
<span class="n">application</span><span class="o">.</span><span class="n">send_notification</span><span class="p">(</span><span class="s2">&quot;app-installed&quot;</span><span class="p">,</span> <span class="n">notification</span><span class="p">)</span>
</pre></div>
</div>
</div><div aria-labelledby="tab-3-VmFsYQ==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-3-VmFsYQ==" name="VmFsYQ==" role="tabpanel" tabindex="0"><div class="highlight-vala notranslate"><div class="highlight"><pre><span></span><span class="kt">string</span><span class="w"> </span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">appname</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot; is now installed&quot;</span><span class="p">;</span><span class="w"></span>
<span class="kd">var</span><span class="w"> </span><span class="n">notification</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Notification</span><span class="w"> </span><span class="p">(</span><span class="n">title</span><span class="p">);</span><span class="w"></span>
<span class="n">notification</span><span class="p">.</span><span class="n">add_button_with_target</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;Launch&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;app.launch&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;s&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">appid</span><span class="p">);</span><span class="w"></span>
<span class="n">application</span><span class="p">.</span><span class="n">send_notification</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;app-installed&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">notification</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-3-SmF2YVNjcmlwdA==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-3-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tabpanel" tabindex="0"><div class="highlight-js notranslate"><div class="highlight"><pre><span></span><span class="kd">const</span><span class="w"> </span><span class="nx">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">appname</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">&quot; is now installed&quot;</span><span class="p">;</span><span class="w"></span>
<span class="kd">const</span><span class="w"> </span><span class="nx">notification</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Gio</span><span class="p">.</span><span class="nx">Notification</span><span class="p">();</span><span class="w"></span>
<span class="nx">notification</span><span class="p">.</span><span class="nx">set_title</span><span class="p">(</span><span class="nx">title</span><span class="p">);</span><span class="w"></span>
<span class="nx">notification</span><span class="p">.</span><span class="nx">add_button_with_target</span><span class="p">(</span><span class="w"></span>
<span class="w"> </span><span class="s2">&quot;Launch&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w"> </span><span class="s2">&quot;app.launch&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w"> </span><span class="nx">GLib</span><span class="p">.</span><span class="nx">Variant</span><span class="p">.</span><span class="nx">new_string</span><span class="p">(</span><span class="nx">appid</span><span class="p">),</span><span class="w"></span>
<span class="p">);</span><span class="w"></span>
<span class="nx">application</span><span class="p">.</span><span class="nx">send_notification</span><span class="p">(</span><span class="s2">&quot;app-installed&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">notification</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div></div>
<p>To make this work, your application needs to have a suitable launch action
that takes the application id as a string parameter:</p>
<div class="sphinx-tabs docutils container">
<div aria-label="Tabbed content" class="closeable" role="tablist"><button aria-controls="panel-4-Qw==" aria-selected="true" class="sphinx-tabs-tab code-tab group-tab" id="tab-4-Qw==" name="Qw==" role="tab" tabindex="0">C</button><button aria-controls="panel-4-UHl0aG9u" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-4-UHl0aG9u" name="UHl0aG9u" role="tab" tabindex="-1">Python</button><button aria-controls="panel-4-VmFsYQ==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-4-VmFsYQ==" name="VmFsYQ==" role="tab" tabindex="-1">Vala</button><button aria-controls="panel-4-SmF2YVNjcmlwdA==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-4-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tab" tabindex="-1">JavaScript</button></div><div aria-labelledby="tab-4-Qw==" class="sphinx-tabs-panel code-tab group-tab" id="panel-4-Qw==" name="Qw==" role="tabpanel" tabindex="0"><div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">GActionEntry</span><span class="w"> </span><span class="n">actions</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s">&quot;launch&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">launch_application</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;s&quot;</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w"> </span><span class="c1">// ...</span>
<span class="p">};</span><span class="w"></span>
<span class="n">g_action_map_add_action_entries</span><span class="w"> </span><span class="p">(</span><span class="n">G_ACTION_MAP</span><span class="w"> </span><span class="p">(</span><span class="n">application</span><span class="p">),</span><span class="w"></span>
<span class="w"> </span><span class="n">actions</span><span class="p">,</span><span class="w"> </span><span class="n">G_N_ELEMENTS</span><span class="w"> </span><span class="p">(</span><span class="n">actions</span><span class="p">),</span><span class="w"></span>
<span class="w"> </span><span class="n">application</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-4-UHl0aG9u" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-4-UHl0aG9u" name="UHl0aG9u" role="tabpanel" tabindex="0"><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># The &quot;launch_application&quot; function is defined elsewhere</span>
<span class="n">action</span> <span class="o">=</span> <span class="n">Gio</span><span class="o">.</span><span class="n">SimpleAction</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;launch&quot;</span><span class="p">)</span>
<span class="n">action</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;activate&quot;</span><span class="p">,</span> <span class="n">launch_application</span><span class="p">)</span>
<span class="n">application</span><span class="o">.</span><span class="n">add_action</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
</pre></div>
</div>
</div><div aria-labelledby="tab-4-VmFsYQ==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-4-VmFsYQ==" name="VmFsYQ==" role="tabpanel" tabindex="0"><div class="highlight-vala notranslate"><div class="highlight"><pre><span></span><span class="c1">// the &quot;launch_application&quot; function is defined elsewhere</span>
<span class="n">ActionEntry</span><span class="w"> </span><span class="n">actions</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s">&quot;launch&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">launch_application</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w"> </span><span class="c1">// ...</span>
<span class="p">};</span><span class="w"></span>
<span class="n">application</span><span class="p">.</span><span class="n">add_action_entries</span><span class="w"> </span><span class="p">(</span><span class="n">actions</span><span class="p">,</span><span class="w"> </span><span class="n">application</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-4-SmF2YVNjcmlwdA==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-4-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tabpanel" tabindex="0"><div class="highlight-js notranslate"><div class="highlight"><pre><span></span><span class="c1">// the &quot;launch_application&quot; function is defined elsewhere</span><span class="w"></span>
<span class="kd">const</span><span class="w"> </span><span class="nx">action</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Gio</span><span class="p">.</span><span class="nx">SimpleAction</span><span class="p">({</span><span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;launch&quot;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="nx">action</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="s2">&quot;activate&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">launch_application</span><span class="p">);</span><span class="w"></span>
<span class="nx">application</span><span class="p">.</span><span class="nx">add_action</span><span class="p">(</span><span class="nx">action</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div></div>
</section>
<section id="stale-notifications">
<h2>Stale notifications<a class="headerlink" href="#stale-notifications" title="Permalink to this headline">#</a></h2>
<p>Sometimes, a notification is no longer relevant and should not persist any
longer. In those cases, you can explicitly withdraw it, like this:</p>
<div class="sphinx-tabs docutils container">
<div aria-label="Tabbed content" class="closeable" role="tablist"><button aria-controls="panel-5-Qw==" aria-selected="true" class="sphinx-tabs-tab code-tab group-tab" id="tab-5-Qw==" name="Qw==" role="tab" tabindex="0">C</button><button aria-controls="panel-5-UHl0aG9u" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-5-UHl0aG9u" name="UHl0aG9u" role="tab" tabindex="-1">Python</button><button aria-controls="panel-5-VmFsYQ==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-5-VmFsYQ==" name="VmFsYQ==" role="tab" tabindex="-1">Vala</button><button aria-controls="panel-5-SmF2YVNjcmlwdA==" aria-selected="false" class="sphinx-tabs-tab code-tab group-tab" id="tab-5-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tab" tabindex="-1">JavaScript</button></div><div aria-labelledby="tab-5-Qw==" class="sphinx-tabs-panel code-tab group-tab" id="panel-5-Qw==" name="Qw==" role="tabpanel" tabindex="0"><div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">now_is_tea_time</span><span class="w"> </span><span class="p">())</span><span class="w"></span>
<span class="w"> </span><span class="n">g_application_withdraw_notification</span><span class="w"> </span><span class="p">(</span><span class="n">application</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;lunch-is-ready&quot;</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-5-UHl0aG9u" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-5-UHl0aG9u" name="UHl0aG9u" role="tabpanel" tabindex="0"><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">now_is_tea_time</span><span class="p">():</span>
<span class="n">application</span><span class="o">.</span><span class="n">withdraw_notification</span><span class="p">(</span><span class="s2">&quot;lunch-is-ready&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div><div aria-labelledby="tab-5-VmFsYQ==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-5-VmFsYQ==" name="VmFsYQ==" role="tabpanel" tabindex="0"><div class="highlight-vala notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">now_is_tea_time</span><span class="w"> </span><span class="p">())</span><span class="w"></span>
<span class="w"> </span><span class="n">application</span><span class="p">.</span><span class="n">withdraw_notification</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;lunch-is-ready&quot;</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div><div aria-labelledby="tab-5-SmF2YVNjcmlwdA==" class="sphinx-tabs-panel code-tab group-tab" hidden="true" id="panel-5-SmF2YVNjcmlwdA==" name="SmF2YVNjcmlwdA==" role="tabpanel" tabindex="0"><div class="highlight-js notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">now_is_tea_time</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="nx">application</span><span class="p">.</span><span class="nx">withdraw_notification</span><span class="p">(</span><span class="s2">&quot;lunch-is-ready&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
</div></div>
</section>
<section id="disabling-notifications">
<h2>Disabling notifications<a class="headerlink" href="#disabling-notifications" title="Permalink to this headline">#</a></h2>
<p>If your application uses notifications, you should allow users to disable them.</p>
<p>GNOME has a blanket “do not disturb” mode, but each application can be
individually controlled through the “Notifications” settings panel.</p>
<p>In order to make your application appear in the panel, add the following line to
your desktop file:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">X</span><span class="o">-</span><span class="n">GNOME</span><span class="o">-</span><span class="n">UsesNotifications</span><span class="o">=</span><span class="n">true</span>
</pre></div>
</div>
</section>
</section>
</article>
<footer>
<div class="related-pages">
<a class="next-page" href="themed-icons.html">
<div class="page-info">
<div class="context">
<span>Next</span>
</div>
<div class="title">Themed Icons</div>
</div>
<svg><use href="#svg-arrow-right"></use></svg>
</a>
<a class="prev-page" href="save-state.html">
<svg><use href="#svg-arrow-right"></use></svg>
<div class="page-info">
<div class="context">
<span>Previous</span>
</div>
<div class="title">Saving and Loading Window State</div>
</div>
</a>
</div>
</footer>
</div>
<aside class="toc-drawer">
<div class="toc-sticky toc-scroll">
<div class="toc-title-container">
<span class="toc-title">
Contents
</span>
</div>
<div class="toc-tree-container">
<div class="toc-tree">
<ul>
<li><a class="reference internal" href="#">Using Notifications</a><ul>
<li><a class="reference internal" href="#prerequisites">Prerequisites</a></li>
<li><a class="reference internal" href="#anatomy-of-a-notification">Anatomy of a notification</a></li>
<li><a class="reference internal" href="#creating-a-notification">Creating a notification</a></li>
<li><a class="reference internal" href="#adding-actions">Adding actions</a></li>
<li><a class="reference internal" href="#actions-with-parameters">Actions with parameters</a></li>
<li><a class="reference internal" href="#stale-notifications">Stale notifications</a></li>
<li><a class="reference internal" href="#disabling-notifications">Disabling notifications</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</aside>
</main>
</div><script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/scripts/furo.js"></script>
<script src="../_static/tabs.js"></script>
</body>
</html>