3

I'm developing a chrome extension and i'm currently making use of the chrome.notifications API. The behaviour i need, is to have each notification have a specific and possibly different url to open on click.

With the chrome rich notifications i can create a notification via chrome.notifications.create(string notificationId, NotificationOptions options, function callback) but the Options parameter doesn't allow me to send in extra information, like the url i want it to open.

What's the best way to do this?

I could use the chrome.notifications.onClicked.addListener(function(string notificationId) {...}) function with the notificationId and find out what the url is, but either i make another call to the server to figure it out (which i want to avoid) or i could probably store it in the local storage, but the easier way would be just to create the notification with that information when i receive it from the server, instead of storing it or getting it again from the server only when the click happens.

I could use the Html5 Notifications, and do something like this:

var notification = new Notification('Notification title', {
  icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
  body: "Hey there! You've been notified!",
});

notification.onclick = function () {
  window.open("http://stackoverflow.com/a/13328397/1269037");

but i would lose the RichNotification features and i'm also trying to avoid going that way.

Is there a simpler or better way?

Cheers

tiagosilva
  • 1,695
  • 17
  • 31

1 Answers1

3

notificationId can be an arbitrary string, so you could use the URL as the notificationId in chrome.notifications.create. If the URL is not necessarily unique, add a prefix or suffix. If you want to have more parameters (e.g. different actions for each button), then serialize the object (e.g. using JSON.serialize). All events listener (onClosed, onClicked, onButtonClicked) receive the notificationId as a parameter, so it's easy to get the original data back.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Yes, that's a great simple idea. I passed a Json as the id and then i can do whatever i need when the notification is clicked, thx – tiagosilva Nov 24 '15 at 11:53