この記事はまだボランティアによって 日本語 に翻訳されていません。ぜひ MDN に参加して翻訳を手伝ってください!
Creates and displays a notification.
Pass a notifications.NotificationOptions
to define the notification's content and behavior.
You can optionally provide an ID for the notification. If you omit the ID, an ID will be generated. You can also pass a callback, which will be called with the notification's ID if the notification was successfully created. You can use the ID to update
or clear
the notification.
If you call chrome.notifications.create()
more than once in rapid succession, Firefox may end up not displaying any notification at all. Waiting to make subsequent calls until within the callback
function is not a sufficiently long delay to prevent this from happening.
Syntax
chrome.notifications.create( id, // string options, // NotificationOptions function( // optional function id // string ) {...} )
This API is also available as browser.notifications.create()
in a version that returns a promise.
Parameters
id
Optionalstring
. This is used to refer to this notification innotifications.update()
,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 add-on, then the other notification will be cleared.options
notifications.NotificationOptions
. Defines the notification's content and behavior.callback
Optionalfunction
. This function is called when the notification is created and the display process has been started, which is before the notification is actually displayed to the user. The function is passed the following arguments:-
id
string
. The notification's ID.
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Chrome | Edge | Firefox | Firefox for Android | Opera | |
---|---|---|---|---|---|
Basic support | Yes | No | 45.0 | 48.0 | 33 |
Examples
Create and display a basic notification periodically, using an alarm
. Clicking the browser action dismisses the notification.
Note that you'll need the "alarms" permission to create alarms (as well as the "notifications" permission to create notifications).
var cakeNotification = "cake-notification" /* CAKE_INTERVAL is set to 6 seconds in this example. Such a short period is chosen to make the add-on'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; chrome.alarms.create("", {periodInMinutes: CAKE_INTERVAL}); chrome.alarms.onAlarm.addListener(function(alarm) { chrome.notifications.create(cakeNotification, { "type": "basic", "iconUrl": chrome.extension.getURL("icons/cake-48.png"), "title": "Time for cake!", "message": "Something something cake" }); }); chrome.browserAction.onClicked.addListener(function () { chrome.notifications.clear(cakeNotification); });
Display a similar notification, but add buttons naming cakes, and log the selected cake when a button is clicked:
var cakeNotification = "cake-notification" /* CAKE_INTERVAL is set to 6 seconds in this example. Such a short period is chosen to make the add-on'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" } ]; chrome.alarms.create("", {periodInMinutes: CAKE_INTERVAL}); chrome.alarms.onAlarm.addListener(function(alarm) { chrome.notifications.create(cakeNotification, { "type": "basic", "iconUrl": chrome.extension.getURL("icons/cake-48.png"), "title": "Time for cake!", "message": "Something something cake", "buttons": buttons }); }); chrome.browserAction.onClicked.addListener(function () { chrome.notifications.clear(cakeNotification); }); chrome.notifications.onButtonClicked.addListener((id, index) => { chrome.notifications.clear(id); console.log("You chose: " + buttons[index].title); });
Example add-ons
This API is based on Chromium's chrome.notifications
API.
Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.