Dallari Gianluca
Dallari Gianluca

Reputation: 31

Chrome extension notification shows only once

My goal is to create a notification with a Chrome extension when a value in firebase changes.
With the code below I receive a notification the first time I'm changing the value, but not the following times.

f.on('child_changed', function (snapshot) {
  ignoreInitialData = false;
  var options = {
    type: 'basic',
    iconUrl: '../../icons/green.png',
    title: "Availability notifier",
    message: snapshot.val(),
    contextMessage: "",
    eventTime: Date.now(),
    isClickable: false,
  };
  chrome.notifications.create("child_changed", options, function (notificationID) {
    console.error(chrome.runtime.lastError);
  });
});

Any solutions?

Upvotes: 3

Views: 1934

Answers (2)

juanignaciosl
juanignaciosl

Reputation: 3573

You can also add a timestamp suffix so every notification is different:

var timestamp = new Date().getTime();
var id = 'myid' + timestamp;

Upvotes: 1

Xan
Xan

Reputation: 77591

Since you're using a fixed notification ID, it's updated instead of creating a new one.

If the notification is not closed but hidden in the Chrome notification center, it will not be shown again.

Your options include:

  • Not using a fixed ID (pass "" as ID)
    This will cause both the old and the new notifications to remain until dismissed. Probably not what you want.

  • Closing the old notification just before showing a new one.
    This works well, but is visually jarring if your old notification did not yet disappear. However, you may actually want this effect if you want to emphasize "this is new information"

  • Using a priority change trick to re-show the notification.
    This works best, but is "hacky". We can only hope Chrome API will improve to do this natively.

Upvotes: 8

Related Questions