Notify users

Post messages to a user's system tray using the extensions Notifications API. Start by declaring the "notifications" permission in the manifest.json.

{
"name":"Drink Water Event Popup",
...
"permissions":[
"notifications",
],
...
}

Once the permission is declared, display a notification by calling notifications.create(). The following example is taken from the Drink water event popup sample. It uses an alarm to set a reminder to drink a glass of water. This code shows the triggering of the alarm. Follow the previous link to explore how this is set up.

chrome.alarms.onAlarm.addListener(()=>{
chrome.action.setBadgeText({text:''});
chrome.notifications.create({
type:'basic',
iconUrl:'stay_hydrated.png',
title:'Time to Hydrate',
message:"Everyday I'm Guzzlin'!",
buttons:[{title:'Keep it Flowing.'}],
priority:0
});
});

This code creates a notification on macOS like the following.

A notification on macOS
A notification on macOS.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024年02月15日 UTC.