aboutsummaryrefslogtreecommitdiff
path: root/files/fr/mozilla/add-ons/webextensions/api/notifications/update/index.html
blob: a83f74a6ceb80236feb7e2597a7f2f918dfb0503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
title: notifications.update()
slug: Mozilla/Add-ons/WebExtensions/API/notifications/update
tags:
  - API
  - Add-ons
  - Extensions
  - Method
  - Non-standard
  - Notifications
  - Reference
  - Update
  - WebExtensions
translation_of: Mozilla/Add-ons/WebExtensions/API/notifications/update
---
<div>{{AddonSidebar()}}</div>

<p>Met à jour une notification, compte tenu de son identifiant</p>

<p>C'est une fonction asynchrone qui renvoie une <code><a href="/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise">Promise</a></code>.</p>

<h2 id="Syntaxe">Syntaxe</h2>

<pre class="syntaxbox brush:js">var updating = browser.notifications.update(
  id,                            // string
  options                        // NotificationOptions
)
</pre>

<h3 id="Paramètres">Paramètres</h3>

<dl>
 <dt><code>id</code></dt>
 <dd><code>string</code>. L'ID de la notification à mettre à jour. C'est la même chose que l'ID transmis dans le callback {{WebExtAPIRef('notifications.create()')}}.</dd>
 <dt><code>options</code></dt>
 <dd>{{WebExtAPIRef('notifications.NotificationOptions')}}. Définit le nouveau contenu et le nouveau comportement de la notification.</dd>
</dl>

<h3 id="Valeur_retournée">Valeur retournée</h3>

<p>Une <code><a href="/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise">Promise</a></code> qui sera remplie avec un booléen : <code>true</code> si la notification a été mise à jour, ou <code>false</code> si ce n'est pas le cas (par exemple, parce que la notification référencée par <code>id</code> n'existe pas).</p>

<h2 id="Compatibilité_du_navigateur">Compatibilité du navigateur</h2>

<p>{{Compat("webextensions.api.notifications.update")}}</p>

<h2 id="Exemples">Exemples</h2>

<p>Cet exemple utilise <code>update()</code> pour mettre à jour une notification de progression. Cliquez sur l'action du navigateur pour afficher la notification et lancer un  {{WebExtAPIRef("alarms", "alarm")}}, que nous utilisons pour mettre à jour l'indicateur de progression de la notification.</p>

<p>Notez que vous aurez besoin de la <a href="/fr/Add-ons/WebExtensions/manifest.json/permissions">permission</a> "alarms" pour créer des alarmes (ainsi que de la permission "notifications" pour créer des notifications). Notez également que Firefox ne prend pas en charge l'attribut de <code>progress</code>.</p>

<pre class="brush: js">var cakeNotification = "cake-notification";

/*

CAKE_INTERVAL is set to 0.3 seconds in this example.
Such a short period is chosen to make the extension's behavior
more obvious, but this is not recommended in real life.
Note that in Chrome, alarms cannot be set for less than
a minute.

*/
var CAKE_PREP_INTERVAL = 0.005;

var progress = 0;

browser.alarms.onAlarm.addListener(function(alarm) {
  progress = progress + 10;
  if (progress &gt; 100) {
    browser.notifications.clear(cakeNotification);
    browser.alarms.clear("cake-progress");
  } else {
    browser.notifications.update(cakeNotification, {
      "progress": progress
    });
  }
});

browser.browserAction.onClicked.addListener(function () {
  browser.notifications.getAll((all) =&gt; {
    if (all.length &gt; 0) {
      browser.notifications.clear(cakeNotification);
      return;
    }
    progress = 0;
    browser.notifications.create(cakeNotification, {
      "type": "progress",
      "iconUrl": browser.extension.getURL("icons/cake-48.png"),
      "title": "Your cake is being prepared...",
      "message": "Something something cake",
      "progress": progress
    });
    browser.alarms.create(
      "cake-progress",
      {periodInMinutes: CAKE_PREP_INTERVAL}
    );
  });
});</pre>

<p>{{WebExtExamples}}</p>

<div class="note"><strong>Remerciements :</strong>

<p>Cette API est basée sur l'API Chromium <a href="https://developer.chrome.com/extensions/notifications"><code>chrome.notifications</code></a>.</p>

<p>Les données de compatibilité relatives à Microsoft Edge sont fournies par Microsoft Corporation et incluses ici sous la licence Creative Commons Attribution 3.0 pour les États-Unis.</p>
</div>