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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
---
title: notifications.create()
slug: Mozilla/Add-ons/WebExtensions/API/notifications/create
translation_of: Mozilla/Add-ons/WebExtensions/API/notifications/create
---
<div>{{AddonSidebar()}}</div>
<p>通知を生成、表示します。</p>
<p>Pass a {{WebExtAPIRef("notifications.NotificationOptions")}} to define the notification's content and behavior.</p>
<p>You can optionally provide an ID for the notification. If you omit the ID, an ID will be generated. You can use the ID to {{WebExtAPIRef("notifications.update()", "update")}} or {{WebExtAPIRef("notifications.clear()", "clear")}} the notification.</p>
<p>This is an asynchronous function that returns a <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code>.</p>
<div class="warning">
<p>If you call <code>notifications.create()</code> more than once in rapid succession, Firefox may end up not displaying any notification at all.</p>
</div>
<h2 id="書式">書式</h2>
<pre class="syntaxbox brush:js">var creating = browser.notifications.create(
id, // optional string
options // NotificationOptions
)
</pre>
<h3 id="パラメータ">パラメータ</h3>
<dl>
<dt><code>id</code>{{optional_inline}}</dt>
<dd><code>string</code>. This is used to refer to this notification in {{WebExtAPIRef("notifications.update()")}}, {{WebExtAPIRef("notifications.clear()")}}, and event listeners. If you omit this argument or pass an empty string, then a new ID will be generated for this notification. If the ID you provide matches the ID of an existing notification from this extension, then the other notification will be cleared.</dd>
<dt><code>options</code></dt>
<dd>{{WebExtAPIRef('notifications.NotificationOptions')}}. Defines the notification's content and behavior.</dd>
</dl>
<h3 id="返り値">返り値</h3>
<p>A <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code> that will be fulfilled when the notification is created and the display process has been started, which is before the notification is actually displayed to the user. It is fulfilled with a string representing the notification's ID.</p>
<h2 id="ブラウザ互換性">ブラウザ互換性</h2>
<p>{{Compat("webextensions.api.notifications.create")}}</p>
<h2 id="例">例</h2>
<p>Create and display a basic notification periodically, using an {{WebExtAPIRef("alarms", "alarm")}}. Clicking the browser action dismisses the notification.</p>
<p>Note that you'll need the "alarms" <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions">permission</a> to create alarms (as well as the "notifications" permission to create notifications).</p>
<pre class="brush: js">var cakeNotification = "cake-notification"
/*
CAKE_INTERVAL is set to 6 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_INTERVAL = 0.1;
browser.alarms.create("", {periodInMinutes: CAKE_INTERVAL});
browser.alarms.onAlarm.addListener(function(alarm) {
browser.notifications.create(cakeNotification, {
"type": "basic",
"iconUrl": browser.extension.getURL("icons/cake-96.png"),
"title": "Time for cake!",
"message": "Something something cake"
});
});
browser.browserAction.onClicked.addListener(()=> {
var clearing = browser.notifications.clear(cakeNotification);
clearing.then(() => {
console.log("cleared");
});
});</pre>
<p>Display a similar notification, but add buttons naming cakes, and log the selected cake when a button is clicked:</p>
<pre class="brush: js">var cakeNotification = "cake-notification"
/*
CAKE_INTERVAL is set to 6 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_INTERVAL = 0.1;
var buttons = [
{
"title": "Chocolate"
}, {
"title": "Battenberg"
}
];
browser.alarms.create("", {periodInMinutes: CAKE_INTERVAL});
browser.alarms.onAlarm.addListener(function(alarm) {
browser.notifications.create(cakeNotification, {
"type": "basic",
"iconUrl": browser.extension.getURL("icons/cake-96.png"),
"title": "Time for cake!",
"message": "Something something cake",
"buttons": buttons
});
});
browser.browserAction.onClicked.addListener(()=> {
var clearing = browser.notifications.clear(cakeNotification);
clearing.then(() => {
console.log("cleared");
});
});
browser.notifications.onButtonClicked.addListener((id, index) => {
browser.notifications.clear(id);
console.log("You chose: " + buttons[index].title);
});
</pre>
<p>{{WebExtExamples}}</p>
<div class="note"><strong>Acknowledgements</strong>
<p>This API is based on Chromium's <a href="https://developer.chrome.com/extensions/notifications#method-create"><code>chrome.notifications</code></a> API.</p>
<p>Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.</p>
</div>
|