Gabriel Gabriel - 3 years ago 224
Javascript Question

Open URLs in Chrome Notifications

Sorry if my Question is vague but this is what I am basically trying to do.
My chrome extension app basically looks at a forum and whenever a new thread is posted, a desktop notification shows up. It uses Chrome notifications API.So it looks something like this:

chrome.notifications.create("id0", options,callback)

options is the template so I can make something like:

var options = {
type: "basic",
title: "NEW THREAD!",
message: "I made a new thread blah blah blah",
iconUrl: "url_to_small_icon"

So when a user clicks the notification, it should open a link of that thread.

Question is how should I do this?

At first, I figured I should just add the url under options template (something like options.message = "") and then once chrome.notifications.onClicked.addListener is called, I would grab that link from options, but I don't think that would work as I cannot grab any info on the notification itself other than the notificationID (in this case, it would be 'id0'). (If I'm wrong, please tell me a way to do this. Thanks!)

A way I thought of is to have an array of the URL of the new threads that my chrome extension app found (Let's say 3 new threads has been posted) and then once chrome.notifications.onClicked.addListener is called, I would check the notification id (in this case id0), and then find the value of the array using the index (in this case, 0) and then open that URL. This does work, but I need to delete that element at some point as my array will increase as we see more new threads.

The problem is, if I delete that element right after I clicked the Notification, I shift the array (The array had a size of 3, but now it'll have 2) and when a user clicks another notification, (let's say notification 'id1'), that will open an element that does not have the proper URL (it should open array(0), not array(1) because I deleted the first element).

Note That the user can click id0, id1 or id2 at any order so I cannot delete the element in the notification if the notifications are displayed.

One thing I could try to do is delete the array once all the notification has been clicked, however I dislike this method....

I'm sure there is a better way so I was hoping you guys can help me out?

Chrome Notifications example:
chrome.notifications api:

Answer Source

Use the forum URL as the notificationId and you won't have to use an array at all.

For example:

var forumUrl = '';
var options = {
  type: "basic",
  title: "NEW THREAD!",
  message: "I made a new thread blah blah blah",
  iconUrl: "url_to_small_icon"

// create notification using forumUrl as id
chrome.notifications.create(forumUrl, options, function(notificationId){ }); 

// create a on Click listener for notifications
chrome.notifications.onClicked.addListener(function(notificationId) {
  chrome.tabs.create({url: notificationId});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download