Ido Barash
Ido Barash

Reputation: 5122

Chrome extension - wake up on timer

I am developing a chrome extension. I need it to wake up every X minutes and display a div. It should happen event if the user did not press on the extension icon. much like the gmail "new mail" notice.

How can I do that?

Upvotes: 1

Views: 285

Answers (2)

kundrata
kundrata

Reputation: 679

I do it like this:

var options = {
  type: "basic",
  title: "Notice me sempai",
  message: "Your message to the user",
  iconUrl: "your_custom_icon.png" 
};

function display_notification () {
  chrome.notifications.create("watch", options, function(notif_id){
    // Store notif_id if you need it
  });
}

var interval = setInterval(display_notification, 30 * 1000); // 30 seconds interval

chrome.notifications.onClicked.addListener(function (not_ID) {
    // Do something 
});

The icon you provide needs to be declared in your manifest file.

There is also more than 1 type of notification you can create. See https://developer.chrome.com/apps/notifications#type-TemplateType It's also the page for all the info on notifications.

Upvotes: 1

abielita
abielita

Reputation: 13469

You may check the chrome.alarms API to schedule code to run periodically or at a specified time in the future. In order to reduce the load on the user's machine, Chrome limits alarms to at most once every 1 minute but may delay them an arbitrary amount more. That is, setting delayInMinutes or periodInMinutes to less than 1 will not be honored and will cause a warning. when can be set to less than 1 minute after "now" without warning but won't actually cause the alarm to fire for at least 1 minute. Here's an example.

Upvotes: 2

Related Questions