Google 丰富的通知,点击打开特定通知的最佳方式 url?
Google Rich Notifications, best way to click to open a notifications specific url?
我正在开发 chrome 扩展,目前正在使用 chrome.notifications API。
我需要的行为是让每个通知都有一个特定且可能不同的 url 以在点击时打开。
有了 chrome 丰富的通知,我可以通过 chrome.notifications.create(string notificationId, NotificationOptions options, function callback)
创建通知,但是 Options 参数不允许我发送额外的信息,比如我想要的 url打开。
最好的方法是什么?
我可以将 chrome.notifications.onClicked.addListener(function(string notificationId) {...})
函数与 notificationId 一起使用并找出 url 是什么,但是我要么再次调用服务器来弄清楚(我想避免)或者我可以将它存储在本地存储中,但更简单的方法是当我从服务器接收到它时使用该信息创建通知,而不是仅在单击发生时存储它或从服务器再次获取它。
我可以使用 Html5 通知,并执行如下操作:
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("");
但我会失去 RichNotification 功能,我也在努力避免那样做。
有没有更简单或更好的方法?
干杯
notificationId
可以是任意字符串,因此您可以将 URL 用作 chrome.notifications.create
中的 notificationId
。如果 URL 不一定是唯一的,请添加前缀或后缀。如果你想有更多的参数(例如每个按钮的不同动作),那么序列化对象(例如使用JSON.serialize
)。
所有事件监听器(onClosed、onClicked、onButtonClicked)都接收notificationId
作为参数,因此很容易取回原始数据。
我正在开发 chrome 扩展,目前正在使用 chrome.notifications API。 我需要的行为是让每个通知都有一个特定且可能不同的 url 以在点击时打开。
有了 chrome 丰富的通知,我可以通过 chrome.notifications.create(string notificationId, NotificationOptions options, function callback)
创建通知,但是 Options 参数不允许我发送额外的信息,比如我想要的 url打开。
最好的方法是什么?
我可以将 chrome.notifications.onClicked.addListener(function(string notificationId) {...})
函数与 notificationId 一起使用并找出 url 是什么,但是我要么再次调用服务器来弄清楚(我想避免)或者我可以将它存储在本地存储中,但更简单的方法是当我从服务器接收到它时使用该信息创建通知,而不是仅在单击发生时存储它或从服务器再次获取它。
我可以使用 Html5 通知,并执行如下操作:
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("");
但我会失去 RichNotification 功能,我也在努力避免那样做。
有没有更简单或更好的方法?
干杯
notificationId
可以是任意字符串,因此您可以将 URL 用作 chrome.notifications.create
中的 notificationId
。如果 URL 不一定是唯一的,请添加前缀或后缀。如果你想有更多的参数(例如每个按钮的不同动作),那么序列化对象(例如使用JSON.serialize
)。
所有事件监听器(onClosed、onClicked、onButtonClicked)都接收notificationId
作为参数,因此很容易取回原始数据。